The basic setup: transform position and mouse input
To make a player move toward your mouse in Unity, you need three things: a script that reads where your mouse is pointing, a way to convert that screen position into world coordinates, and a line of code that moves your player toward that spot each frame. The most straightforward approach uses Input.mousePosition to get the mouse location and Camera.main.ScreenToWorldPoint() to translate it into your game world.
Start by creating a new C# script attached to your player object. This script will run every frame, check where the mouse is, and move the player in that direction. The movement happens in the Update() method, which runs once per frame and is the standard place for input handling in Unity.
Key Takeaways
- Use Input.mousePosition to read the mouse location on screen, then convert it to world space with Camera.main.ScreenToWorldPoint().
- Calculate the direction from your player to the mouse by subtracting the player position from the mouse position, then normalize the result so movement speed stays consistent.
- Move the player each frame by adding a velocity vector to the transform position, multiplying by Time.deltaTime to keep speed the same regardless of frame rate.
- Stop the player at the mouse location by checking the distance between player and mouse; when it drops below a threshold, set velocity to zero.
- If your camera is not the default main camera or uses orthographic projection, adjust the ScreenToWorldPoint call to match your setup.
Writing the movement script step by step
Create a new C# script called PlayerMouseMovement.cs and attach it to your player GameObject. At the top of the script, declare two public variables: one for movement speed and one to store the target position. These let you adjust speed in the Inspector without editing code.
Inside the Update() method, first get the mouse position on screen using Input.mousePosition. This returns pixel coordinates. Next, convert those screen coordinates to world space by calling Camera.main.ScreenToWorldPoint(Input.mousePosition). If your game uses a 2D camera, you may need to set the z-coordinate manually to keep the player on the correct plane.
Calculate the direction from the player to the mouse by subtracting the player's current position from the target position. Then normalize this direction vector so that diagonal movement does not move faster than straight movement. Multiply the normalized direction by your speed variable and by Time.deltaTime to move the player smoothly regardless of frame rate.
Here is a working example for a 2D game:
public float speed = 5f; private Vector3 targetPosition; void Update() { Vector3 mouseScreenPos = Input.mousePosition; mouseScreenPos.z = 10f; // Distance from camera targetPosition = Camera.main.ScreenToWorldPoint(mouseScreenPos); Vector3 direction = (targetPosition - transform.position).normalized; transform.position += direction * speed * Time.deltaTime; }
Stopping the player when reaching the mouse
Without a stopping condition, your player will jitter around the mouse cursor because it overshoots and corrects every frame. Add a distance check to halt movement when the player gets close enough to the target.
Before moving the player, calculate the distance between the player and the mouse position. If that distance is smaller than a threshold (try 0.1 units to start), skip the movement code for that frame. This creates a dead zone where the player stops rather than vibrating in place.
Here is the updated code:
void Update() { Vector3 mouseScreenPos = Input.mousePosition; mouseScreenPos.z = 10f; targetPosition = Camera.main.ScreenToWorldPoint(mouseScreenPos); float distanceToMouse = Vector3.Distance(transform.position, targetPosition); if (distanceToMouse > 0.1f) { Vector3 direction = (targetPosition - transform.position).normalized; transform.position += direction * speed * Time.deltaTime; } }
Adjusting for different camera setups
The code above assumes you are using a perspective camera with a z-distance of 10 units. If your game uses an orthographic camera (common in 2D games), you do not need to set the z-coordinate because orthographic projection ignores depth. Remove the line mouseScreenPos.z = 10f and the code will work correctly.
If your camera is not tagged as "MainCamera" in the Inspector, Camera.main will return null and your game will crash. Check the Inspector: select your camera, look at the Tag dropdown in the top right, and make sure it says "MainCamera". If it does not, click the dropdown and select or create that tag.
For games with multiple cameras or a camera that moves, you may want to cache the camera reference at the start instead of calling Camera.main every frame. Add this line near the top of your script:
private Camera mainCamera; void Start() { mainCamera = Camera.main; }
Then replace Camera.main.ScreenToWorldPoint() with mainCamera.ScreenToWorldPoint() in your Update method. This is slightly faster because it avoids a search every frame.
Adding rotation so the player faces the mouse
Movement toward the mouse looks more natural if the player rotates to face the direction it is moving. Calculate the angle between the player and the mouse, then rotate the player to match that angle.
Use Mathf.Atan2() to find the angle in radians, convert it to degrees, and apply it to the player's rotation. For a 2D sprite, you typically only rotate around the z-axis:
Vector3 direction = (targetPosition - transform.position).normalized; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
If your sprite is oriented differently (pointing up instead of right), subtract 90 from the angle before applying it. Test in the Scene view and adjust until the sprite points the correct direction.
Common problems and fixes
The player does not move at all. Check that the script is attached to the player GameObject and that the GameObject has a Rigidbody or Collider component if your scene uses physics. Verify the camera is tagged "MainCamera". Add a Debug.Log statement to confirm the script is running: put Debug.Log("Update running"); at the start of Update and check the Console.
The player moves in the wrong direction or at wrong speed. Make sure you are using Time.deltaTime in the movement calculation. Without it, movement speed depends on frame rate and will vary. If the player moves backward, check that you are subtracting the player position from the target position, not the other way around.
The player jitters or shakes. This usually means the stopping distance is too small or not set at all. Increase the threshold in the distance check from 0.1f to 0.5f and test. If the player still shakes, the issue may be that the mouse position changes every frame even when stationary; the distance check should prevent this, so verify that code is in place.
The player moves off screen or in unexpected directions. This often happens when the z-coordinate in ScreenToWorldPoint is wrong. If you are using a perspective camera, try different z values (5, 10, 15) until movement feels correct. For orthographic cameras, remove the z line entirely.
Frequently Asked Questions
Can I make the player move at a constant speed instead of slowing down near the mouse?
Yes. Remove the distance check and the normalized direction will move the player at constant speed toward the mouse indefinitely. If you want the player to stop at the mouse, keep the distance check but increase the threshold so it triggers sooner.
How do I make the player move only on a 2D plane?
Set the z-coordinate of the target position to match the player's z-coordinate before calculating direction. Add this line after converting screen to world: targetPosition.z = transform.position.z; This keeps movement on the x and y axes only.
What if I want the player to move toward the mouse only when a button is held down?
Wrap the movement code in an if statement that checks for input. For example: if (Input.GetMouseButton(0)) { /* movement code here */ } This runs the movement only when the left mouse button is pressed.
Can I use Rigidbody.velocity instead of changing transform.position directly?
Yes. If your player has a Rigidbody component, you can set rb.velocity = direction * speed; instead of modifying transform.position. This integrates with physics and prevents the player from passing through colliders. Make sure the Rigidbody is set to Kinematic if you do not want gravity to affect it.
How do I slow the player down as it approaches the mouse?
Instead of a hard stop at a threshold distance, multiply the speed by a factor based on distance. For example: float speedMultiplier = Mathf.Clamp01(distanceToMouse / 2f); then use direction * speed * speedMultiplier * Time.deltaTime for movement. This creates a smooth deceleration over the last 2 units.