Make a GameObject move smoothly from one point to another.

Learn how to create a simple animation using Vector3.Lerp and Monobehaviour.Update

Smooth moving circle using Vector3.Lerp

There are hundreds of ways to make gameobjects move in Unity. When you have access to the transform of a gameobject you have several options on how you want to handle movement. The key is to found out which way match the vision for what you want to do.

Sometimes you just don’t want a boring static movement but instead you might wantsome life and something that just feels nice.

An easy solution, which I will propose to you is to use Vector3.Lerp inside the Update method of the gameobject you want to move.

Interpolates between the vectors a and b by the interpolant t. The parameter t is clamped to the range [0, 1]. This is most commonly used to find a point some fraction of the way along a line between two endpoints (e.g. to move an object gradually between those points).

Unity3D Documentation https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html

We can make use of Vector3.Lerp inside the Update method of the gameobject and make use of Time.deltaTime to make the movement smooth.

Here’s how to do it

Create a new Unity project using the 2D template.

Now create a new 2D sprite

Give the sprite a background a sprite image, let’s go with “knob”

Now we have the gameobject we want to move. To determine where we want the gameobject to move from and to, we will create to empty gameobjects which will act as a start point and an end point.

Give the Start point an x-coordinate of -5 and the End point a x-coordinate of 5

Now select the New Sprite gameobject can press on “Add Component” in the inspector.

Select “New Script” and name your script “SmoothMover”.

The script will be responsible for making the Sprite move from the Start point to the End point.

Open up the SmoothMover.cs file in your preferred editor. The default for Unity is Visual Studio but I personally use Rider from Jetbrains.

You are then greeted with the following code:

using UnityEngine;

public class SmoothMover : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

There are already inserted comments to explain the most basic stuff. We need access to the two points in our class so we will create two SerializeFields so that we can assign them in the inspector in the editor.

Add the following lines just before the start method:

[SerializeField] private Transform start;
[SerializeField] private Transform end;

Now save the file and go back to Unity. If you select your sprite gameobject you will see in the Smooth Mover component that you now have two new fields.

Now you can drag the “Start” gameobject to the “Start” field and the “End” gameobject to the “End” field.

To actually visualize where these two transforms are (I’m talking about transforms because our Start and End gameobject only has the transform component) we can draw Gizmos at their position.

Because we now have a reference to the transforms in the SmoothMover script and because SmoothMover is a Monobehaviour we can use a method called OnDrawGizmos

Inside the OnDrawGizmos method you have access to the Gizmo object which you can use to draw cubes inside your game world. These are used for debugging purposes.

Write the following method:

private void OnDrawGizmos()
{
    Gizmos.DrawCube(start.position,Vector3.one*0.1f);
    Gizmos.DrawCube(end.position,Vector3.one*0.1f);
}

Gizmos.DrawCube has two inputs. A position and a scale. They must both be Vector3’s.

Remember to save the file. Now back inside the Unity editor make sure you have gizmos enabled.

You can now see the two transforms in the game view.

Now inside the Start method we will set the position of our sprite gameobject to the position of the Start transform.

private void Start()
{
transform.position = start.position;
}

If you click on Play in Unity you will see that the sprite instantly moves to the Start transform.

The final thing we have to do is to move the sprite from start to end using the Vector2.Lerp method. Yes so far I’ve mentioned Vector3.Lerp but they are identical except for the Z-axis which we don’t need to use in 2D.

Inside Update write the following code:

void Update()
{
transform.position = Vector2.Lerp(transform.position, end.position, Time.deltaTime);
}

As we talked about in the beginning the code takes the sprite from transform.position to end.position by Time.deltaTime amount. You can multiply Time.deltaTime with a number to make the movement go faster.

Your code should now look like this:

using UnityEngine;

public class SmoothMover : MonoBehaviour
{
    [SerializeField] private Transform start;
    [SerializeField] private Transform end;
    private void Start()
    {
        transform.position = start.position;
    }

    void Update()
    {
        transform.position = Vector2.Lerp(transform.position, end.position, Time.deltaTime);
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawCube(start.position,Vector3.one*0.1f);
        Gizmos.DrawCube(end.position,Vector3.one*0.1f);
    }
}

Congratulations!

You did it! I hope you found this post useful. Remember I have videos on Youtube as well https://www.youtube.com/channel/UC-bNkJnkLW-_DCMU4puN6aw.

Have a great day 🙂

2 thoughts on “Make a GameObject move smoothly from one point to another.

Leave a comment