In many games, you might need an enemy, follower, or NPC to chase the player or a target object. Unity provides several ways to achieve this, and in this article, we’ll focus on one simple yet effective method: the Vector3.MoveTowards()
function.
This approach allows you to smoothly move one GameObject toward another at a controlled speed, making it ideal for basic AI movement, following mechanics, or even simple pathfinding. We’ll break down how it works, how to implement it in a script, and how to customize its behavior.
For a visual breakdown of this subject, you may find my video discussion helpful.
To begin, create a new C# script in Unity by right-clicking in the Project Window and selecting Create > C# Script (See Figure 1). Name it (e.g., ChaseTarget
), then double-click to edit it in your code editor.


Next, drag the script from the Project Window onto an empty GameObject in your scene’s Hierarchy. This assigns the script as a component, allowing you to define movement logic (like MoveTowards
) that will execute during gameplay.
How to Move an Object to a Target Position in Unity (Using C# Scripting)
In your script, define two public GameObject
variables to reference the moving object and its target (See Figure 3) and a float variable for the speed of the chasing object:
public GameObject sphereObject; // The object that will move (e.g., a sphere)
public GameObject targetPosition; // The target to chase
public float speed = 1f; //The speed of the chasing object

After saving the script, return to Unity’s Editor (See Figure 4 for visual reference):
- Select the GameObject with your script attached.
- In the Inspector, you’ll see the two empty fields for
sphereObject
andtargetPosition
. - Drag and drop the respective GameObjects from your scene or Hierarchy into these slots.
Now your script can access these objects at runtime! (Tip: Mark variables as [SerializeField]
if you prefer private fields to appear in the Inspector.)

Movement instructions and safety checks
To move the sphereObject
toward the targetPosition
, use Vector3.MoveTowards()
inside an update method (e.g., Update()
, FixedUpdate()
, or LateUpdate()
). Add the speed
variable to control movement and multiply it by Time.deltaTime
for frame-rate independence:
void Update()
{
if (targetPosition != null)
{
sphereObject.transform.position = Vector3.MoveTowards(
sphereObject.transform.position,
targetPosition.transform.position,
speed * Time.deltaTime
);
}
}
About the Update functions
- Update Methods:
Update()
: Standard for most movement (runs every frame).FixedUpdate()
: Preferred for physics-based movement (syncs with physics steps).LateUpdate()
: Useful if movement should follow other calculations (e.g., camera tracking).
- Safety Check: Always validate
targetPosition
exists to avoidNullReferenceException
.