How to set up mouse-controlled movement in Unity

To make a player move when you click the mouse in Unity, you need three things: a player object with a script attached, code that detects mouse clicks, and code that moves the player toward that point. The simplest approach uses raycasting — a technique that shoots an invisible line from your camera through the mouse position to find where the player should go.

The basic flow works like this: when you click, Unity checks what the mouse is pointing at. If it hits the ground or a target area, your script calculates the distance to that spot and moves the player there over time. This feels responsive because the player starts moving when ready and stops when they reach the destination.

Key Takeaways

  • Raycasting detects where the mouse is pointing in the game world by shooting an invisible line from the camera through the mouse position.
  • You need a script on your player object that listens for mouse clicks and calculates the target position using Physics.Raycast.
  • Movement happens by changing the player's position each frame using Vector3.Lerp or by adding velocity, not by teleporting when ready.
  • A ground layer or target layer tells the raycast what objects to detect, so clicks on the sky or other objects do not trigger movement.
  • Testing requires a camera in your scene, a player object, and a ground plane or collider for the raycast to hit.

Setting up your player object and script

Start by creating a player object if you do not have one — a straightforward cube or capsule works fine for testing. Add a Collider component to it (a Capsule Collider is standard for characters). Then create a new C# script, attach it to the player, and name it something like PlayerMovement.

You also need a ground plane or floor for the raycast to detect. Create a plane in your scene, scale it up, and add a Box Collider to it. This is what the raycast will hit when you click. Assign both the player and ground to a layer — go to the Layers dropdown in the Inspector and create a "Ground" layer, then assign the ground plane to it. This tells your script which objects to detect.

Writing the raycast and click detection code

Open your PlayerMovement script and add the basic structure. At the top, declare a variable for movement speed and a variable to store the target position. Then, in the Update method, check if the mouse button is pressed using Input.GetMouseButtonDown(0) — the 0 means the left mouse button.

Inside that check, create a raycast. The raycast starts at the camera position and shoots through the mouse position into the world. Here is the pattern:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 1000f, LayerMask.GetMask("Ground"))) {   targetPosition = hit.point; }

This code creates a ray from the camera through the mouse, checks if it hits something on the Ground layer within 1000 units, and stores the hit point as the target. The hit.point is the exact spot in the world where the raycast touched the ground.

Moving the player toward the target

Once you have the target position, you need to move the player there. The most common method is Vector3.Lerp, which smoothly moves the player from their current position to the target over time. Add this to your Update method after the raycast:

if (Vector3.Distance(transform.position, targetPosition) > 0.1f) {   transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * speed); }

This checks if the player is close enough to the target (within 0.1 units). If not, it moves the player a small amount each frame toward the target. The Time.deltaTime ensures movement is smooth regardless of frame rate, and speed controls how fast the player moves. Adjust speed to a value like 5 to start — higher numbers move faster.

An alternative is to use a Rigidbody and explore velocity instead of changing position directly. This works better if your player has physics interactions like jumping or collision knockback. Use GetComponent to access the Rigidbody, then set its velocity toward the target each frame.

Preventing clicks from moving the player through walls

If your scene has obstacles, the raycast will still hit the ground behind them. To stop the player from moving through walls, add colliders to your obstacles and make sure they are not on the Ground layer. The raycast only detects the Ground layer, so it stops at walls instead of passing through them.

Another option is to use pathfinding — a system that calculates a safe route around obstacles instead of a straight line. Unity's built-in NavMesh system handles this. You would still raycast to find where the player clicked, but instead of moving in a straight line, you use NavMeshAgent to follow a path that avoids walls. This is more complex but necessary for games with many obstacles.

Testing and common problems

Before testing, make sure your camera is tagged as "MainCamera" — the code looks for Camera.main, which finds the camera with that tag. If the raycast does not work, the camera is usually the problem. Also check that your ground plane has a collider and is on the Ground layer.

If the player moves but feels jerky or stops before reaching the target, adjust the speed variable or the distance check (the 0.1f value). If clicks do nothing, add a Debug.Log inside the raycast check to confirm it is being hit. If the player moves through walls, check that obstacles have colliders and are not on the Ground layer.

Test by clicking different spots on the ground and watching the player move. The player should start moving when ready when you click and stop when they get close to the target. If movement is too fast or too slow, change the speed value until it feels right.

Frequently Asked Questions

Can I make the player face the direction they are moving?

Yes. Calculate the direction using Vector3.Normalize(targetPosition - transform.position), then use transform.LookAt(targetPosition) to rotate the player toward that point. Do this in the same Update method where you move the player.

What if I want the player to stop moving when I click on them?

Add a check in your raycast code: if (hit.collider.gameObject == gameObject) then do not set the target. This prevents the player from moving to their own position when you click on them.

How do I make the player move faster or slower?

Change the speed variable in your script. Higher numbers move faster — try values between 2 and 10 to start. You can also expose this as a public variable in the Inspector so you can adjust it without editing code.

Can I use this same method for clicking on enemies or objects?

Yes. Instead of only detecting the Ground layer, create separate layers for enemies or objects and check for those too. You can raycast multiple layers by combining them with the bitwise OR operator in LayerMask.

What is the difference between Lerp and velocity-based movement?

Lerp directly changes position and is simpler for basic movement. Velocity uses physics, so it works better if your player jumps, gets knocked back, or interacts with other physics objects. For a straightforward click-to-move game, Lerp is usually enough.