MoveTowards In Unity

Liberty Depriest
2 min readAug 12, 2022

--

Goal: get powerups quickly moving towards the player

A simple way to achieve this is to use Vector2.MoveTowards. This involves getting the current position to the target’s position.

In our 2D SpaceShooter game, we use Vector2, as in we only use two axis here.

In our powerup script, we first need to get a reference to the target, which is the player, and create a bool defining when the powerups move to the player:

in Start

Once we press C, we start moving the powerups to the player. In the MoveTowards method, we pass in our current position, the target position, and at what rate, which is our speed * Time.deltaTime.

Now, this format is also the same in 3D games. Except it’s used with Vector3.

It would be:

transform.position = Vector3.moveTowards(transform.position, _player.transform.position, _speed * Time.deltaTime);

And this is what we get:

And that’s all for this short article, but hope it was helpful! Have a great game-dev day!

--

--