A common task is moving one object towards another position in Unity. Assuming you want constant, linear speed, Vector.MoveTowards is a great solution. You can also do lerping covered in this article here.

I experimented with this in an earlier prototype of Puzzledorf to make pieces slide across the board, although in the end I decided snappy movements worked better for a grid-like puzzler – it helps the game to keep up with your brain so you can try lots of solutions quickly. So if you use Vector.MoveTowards, think about the speed of it and trial it.

# Move Towards

The psuedocode is:

`Object to move = MoveTowards( currentPosition, newPosition, speed);`

Example code:

`piece.transform.position = Vector3.MoveTowards(piece.transform.position, newPosition, speed * Time.deltaTime);`

It says, move the object from it’s current position towards the end position at the following speed.

current position is not the objects starting position, but its current position each frame, because each frame it checks the current position and moves it closer towards the end position.

endPosition is simply where want your object to finish moving.

Speed is the speed to move. We multiply speed by deltaTime because, if speed was 1, we would be saying, “We want this object to take one second to move”. Multiplying the speed by delta time makes sure that movement is divided up evenly across the update frames. Increase the speed and the object will move faster.

Make It With Me

It might help some people to make it with me. Otherwise, skip down to the Example Code section.

1. Make a new 2D Unity Project.

2. Create a new “2D Object > Sprites > Square” and place it within the camera view.

3. Create a new script and call it “Move Towards”.

4. Attach the script to your game object in the scene.

```public class MoveTowards : MonoBehaviour
{

public GameObject square;
Vector3 targetDistance = new Vector3(1, 0, 0);
Vector3 newPosition;
float speed = 1f;

bool moving = false;

private void Start()
{
}

void Update()
{
}

}```

square is the square object in your scene that we will refer to in code.

targetDistance is how far we want the object to move. In this case, 1 unit along the X axis.
With the Vector 3, it’s (X, Y, Z) so by leaving Y and Z as 0, we’re saying, only move along the X axis.

You can’t create a Vector 3 by going Vector3 targetDistance = (1, 0, 0); or Vector3 targetDistance = Vector3(1, 0, 0); You need to have the word new in there to let C# know you are creating a new Vector 3.

newPosition is where we want the object to move to. We will leave it blank at the start and set it later.

speed is simply how fast you want it to move.

moving is a true or false variable we will use to determine whether the square is moving or not later.

6. Assign the square game object in your scene to the public variable in the Unity Editor.

Now add this code to Start():

```private void Start()
{
newPosition = square.transform.position + targetDistance;
}```

We set the newPosition to be the square’s current position + the target distance that we set earlier to 1. This means that we will try and move the square 1 unit from wherever it currently is.

Now add this code to Update():

```void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && moving == false)
{
moving = true;
}

if(moving && square.transform.position != newPosition)
{
square.transform.position = Vector3.MoveTowards(square.transform.position, newPosition, speed * Time.deltaTime);
}
}```

Whenever we press space bar, if we are not already moving, we set the variable moving to true.

Then, if moving is true, and the square’s position is not equal to the new position, then, move the square. We move the square by changing it’s current position with the Vector3.MoveTowards() function.

You can test your project now. You will see that the square moves when you press space bar. To make it so that we can move it multiple times, we can add this extra code to the end of our update function (is where the other code is):

```void Update()
{

...

if(square.transform.position == newPosition)
{
moving = false;
newPosition = square.transform.position + targetDistance;
}
}```

So we check if the square has reached it’s new position. If it has, we set moving to false so that we can move it again. We also have to reset the newPosition variable so that we have a new position to move to.

Try it now. Once it stops moving we can move it again.

Example Code

```using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveTowards : MonoBehaviour
{
public GameObject square;
Vector3 targetDistance = new Vector3(1, 0, 0);
Vector3 newPosition;
float speed = 1f;

bool moving = false;

private void Start()
{
newPosition = square.transform.position + targetDistance;
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
moving = true;
}

if(moving && square.transform.position != newPosition)
{
square.transform.position = Vector3.MoveTowards(square.transform.position, newPosition, speed * Time.deltaTime);
}

if(square.transform.position == newPosition)
{
moving = false;
newPosition = square.transform.position + targetDistance;
}
}

}
```