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.
Create a free account, or log in.
Gain access to free articles, game development tools, and game assets.