MoveTowards: The Ultimate Guide

by Michael Sacco Published August 30, 2023
The cover for MoveTowards: The Ultimate Guide

As a game developer using Unity, I always use MoveTowards in my projects. This method is a great tool to create smooth and controlled movement. Unity has implementations for this method in Mathf, Vector2, and Vector3. In this article, I will explain how to use MoveTowards. And I will provide step-by-step examples.

What you need to know

  • MoveTowards makes it easy to move an object from one position to another.
  • Mathf, Vector2, and Vector3 all have MoveTowards implementations.

Syntax for MoveTowards

float MoveTowards(float current, float target, float maxDelta);
Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta);
Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

What is MoveTowards?

When you call MoveTowards, you provide a few values.

  • The current value.
  • The target value.
  • A maximum “speed.” The top speed controls how fast you will move each frame. Unity calls this the maxDelta or maxDistanceDelta.

This method takes the current value and moves it towards the target value. It will move towards the target value at the rate you specify as the max delta. If it’s close, it will move less than the max delta. That way, it reaches the target without overshooting.

This method helps objects move between two spots at a chosen speed.

How to use MoveTowards?

I’ll give three examples, one for each MoveTowards implementation.

Let’s start with the Mathf implementation.

How to use Mathf.MoveTowards

Armored Core 6 inspired this example. In that game, your mech has a certain amount of energy. You can use this energy to perform actions like a quick boost or a rush. It recovers over time when you don’t use the power for a few moments.

In this example, we start by setting the current energy level to maximum.

In Update, we check if the player has tried to boost. If you have enough energy to boost, we reduce your energy by the quick boost cost.

Finally, we recover the energy over time based on the energy recovery rate.

using UnityEngine;

public class FloatExample : MonoBehaviour
{
  public float energy;
  public float maxEnergy = 100f;
  public float energyRecovery = 100f;
  public float quickBoostCost = 30f;
  
  private void Start()
  {
    energy = maxEnergy;
  }
  
  void Update()
  {
    if (Input.GetKeyDown(KeyCode.Q) && energy > quickBoostCost)
    {
      energy -= quickBoostCost;
    }
    
    energy = Mathf.MoveTowards(energy, maxEnergy, energyRecovery * Time.deltaTime);
  }
}

This example focuses on a specific use case for Mathf.MoveTowards - handling an energy meter. But you can also use this method to recover health or other player values. You can also use this with individual transform components. For example, you can use it with transform.position.x or transform.rotation.y.

If you’re wondering about how to come up with game ideas, consider a random game generator to help you brainstorm.

Mathf.MoveTowards interpolates any value towards a target value at a specific rate.

How to use Vector2.MoveTowards

Imagine you’re developing a 2D space shooter game. You want the player’s spaceship to move by itself in your game. It should chase down and destroy enemy ships. You want the player’s spaceship to move towards the target enemy ship. Here’s how to achieve this using the MoveTowards method with Vector2:

using UnityEngine;

public class SpaceshipController : MonoBehaviour
{
  // The target enemy ship
  public Transform targetEnemy;
  // Speed at which the player's spaceship moves
  public float speed = 5.0f;
  
  void Update()
  {
    // Check if there is a target enemy
    if (targetEnemy != null)
    {
      // Get the current position of the player's spaceship (Vector2)
      Vector2 currentPosition = new Vector2(transform.position.x, transform.position.y);
      
      // Get the target position of the enemy ship (Vector2)
      Vector2 targetPosition = new Vector2(targetEnemy.position.x, targetEnemy.position.y);
      
      // Move the player's spaceship towards the enemy at the specified speed
      currentPosition = Vector2.MoveTowards(
        currentPosition,
        targetPosition,
        speed * Time.deltaTime
      );
      
      // Update the player's spaceship position
      transform.position = new Vector3(
        currentPosition.x,
        currentPosition.y,
        transform.position.z
      );
    }
  }
}

Let’s break this code down. The player’s spaceship moves towards the target enemy ship at the specified speed. If the enemy changes position, the spacecraft will course-correct.

As you can see, using Vector2.MoveTowards makes it easy to “move towards” an object in your scene.

With this method, you don’t need to calculate the direction or movement vectors. And you don’t need to handle overshoot.

How to use Vector3.MoveTowards

You will use the Vector3 implementation of MoveTowards a lot. This implementation is the easiest to use in context with transform properties.

This method’s most common use case is moving an object in 3D space. Imagine a game where you want a car AI to move towards the player. Here’s how you can do it:

using UnityEngine;

public class CarMovement : MonoBehaviour
{
  public Transform player;
  public float speed = 30.0f;
  
  void Update()
  {
    // Move the car towards the target at the specified speed
    transform.position = Vector3.MoveTowards(
      transform.position,
      player.position,
      speed * Time.deltaTime
    );
  }
}

In this example, the car moves towards the player’s position. Each second, it moves a total distance equal to the “speed” variable. Using deltaTime guarantees that the movement is frame-rate independent.

Should I use deltaTime with MoveTowards?

Yes. You need to use deltaTime with this method. If you don’t use deltaTime with MoveTowards, your character movement will depend on your frame rate.

Understanding the Difference: Mathf, Vector2, and Vector3

Mathf, Vector2, and Vector3 all serve similar purposes. However, each type has a distinct use case. Let’s dive into the differences between these three and when to use each.

Mathf

Mathf is a utility class in Unity. It deals with everyday mathematical operations and functions. It’s handy for handling single values like floats and integers. You can use Mathf for tasks such as the following:

  • Smooth interpolation (Lerp).
  • Clamping values within a range.
  • Calculating distances.

Mathf is your go-to tool when working with single values.

Vector2

Vector2 is a 2D vector class in Unity. It represents a point or direction in a 2D space, often used in 2D games. A Vector2 has two components, x and y. These components represent positions or directions along the x and y axes.

  • Vector2 makes it easy to move in two dimensions.
  • Vector2 includes helpful 2D coordinate math functions.

Vector3

Vector3 is a 3D vector class in Unity. It extends the concept of Vector2 to three dimensions, adding the z component. This class is perfect for handling 3D positions, directions, and movements. If you’re working on 3D games or need to handle objects in a 3D space, Vector3 is the tool you need. It’s essential for character movement, object positioning, camera control, etc.

Try it yourself

You now have a solid understanding of the MoveTowards method. Now, it is time to practice.

Imagine you are creating a 3D space exploration game. Your player’s spaceship has to move toward asteroids. You will make these asteroids in random positions in the scene. These asteroids will move at different speeds. Here’s what you need to do:

  • Create a new Unity project or use an existing one if you prefer.
  • Design a 3D space environment with a spaceship and several asteroid models.
  • Create a C# script for the spaceship.
  • Use the Vector3.MoveTowards method to move the spaceship towards the asteroids.
  • Make a system that creates new asteroid targets. The system should create new asteroids over time. Each asteroid should spawn at a random position.
  • Experiment with different speed values for the spaceship. How does it affect the gameplay experience?
  • Make sure that your spaceship’s movement is frame-rate independent.
  • Extra credit: Use Mathf to interpolate other game values. Try changing the spaceship’s energy level. Then, see if you can adjust the size of the asteroids. These features help make your game feel complete.

FAQs

Does MoveTowards automatically move the object?

No. MoveTowards is a utility function that returns a value. You need to use this value to set the property in your script.

Should I use Time.deltaTime with MoveTowards?

Yes. MoveTowards is not frame-rate independent. You need to use Time.deltaTime to normalize the change per second.

Do I need to normalize vectors when using MoveTowards?

In general, no. If you use MoveTowards with absolute positions, you do not need to normalize the vectors. You should normalize the vectors if you use MoveTowards to interpolate a vector as an angle. Consider using MoveTowardsAngle for angle interpolation. Learn more about normalization in my beginner-friendly guide on how to normalize a vector.

Do I need to call MoveTowards in Update or Start?

Call MoveTowards from Update. MoveTowards is an iterative process. Each time you call it, you move towards the target. You need to call it many times to reach the target. Read my comprehensive guide to Late Update in Unity for more context on the event functions.

Is MoveTowards the same as Lerp?

No. MoveTowards interpolates from A to B. Lerp also interpolates from A to B. But, the way that these two methods interpolate is different. MoveTowards interpolates a set amount. Lerp interpolates by a percentage. Read my guide, about using Lerp in Unity, to learn more about that method.

Recommended Reading

Conclusion

In conclusion, MoveTowards is a powerful tool in Unity. You can use it to create smooth and controlled movement. It is a versatile method that you can use in a variety of situations, from moving objects in 2D or 3D space to animating characters or objects. Now that you understand how MoveTowards works, you can create better games that will keep your players engaged.

Free download: Indie Game Marketing Checklist

Download now

Category

unity basics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free