How to follow mouse in Unity 2D

Interaction with mouse is a very common feature in most of the games and sometimes, you need to move the player to the mouse position. This feature is widely used in 2D games. In this tutorial, we will see how to make a 2D game object follow the mouse.

Step1: Adding the player to the scene

For the sake of this tutorial, let’s use the default 2D triangle.

  • Go to the hierarchy window, click on the + sign and go to 2D object>Sprites>Triangle.
  • Reset the transform of the triangle to be at (0,0,0).
  • Select the triangle and add a script called “MouseFollow” to it.
  • Open the script for editing.
Adding a 2D triangle to the scene

Step2: Getting the mouse position

If you are using the old input system then use Input.mousePosition, and if you are using the new input system use Mouse.current.position.ReadValue(). You can refer our tutorial on mouse position for more details.

Now you need to set the Z value of mouse position in both the cases. You can set the z value to the camera near field clip using the code below.

Vector3 pos=Input.mousePosition;
pos.z=Camera.main.nearClipPlane;
Mouse position

The mouse position we have, is in screen space. So, we need to convert it into world space. Unity has a public class for that, here is how to use it.

Vector2 worldPos=Camera.main.ScreenToWorldPoint(pos);

If you convert from screen to world without setting the z position of the mouse, then you will get a wrong value. So, keep that in mind.

Step3: Making the player turn towards the mouse position

Before we make the player follow the mouse position, we need to make the player look towards the mouse position.

We will be using Quaternion.LookRotation for this. Quaternion.LookRotation function, takes in the direction as the first input and the axis of rotation as the second input.

Here is the code sample to make the game object look towards the mouse position.

transform.rotation=Quaternion.LookRotation(distancefromMouse,Vector3.forward);

Step4: Making the game object follow the mouse position

The last step is to update the player position based on mouse position. Using transform.position will instantly teleport the object to mouse position. If that is something you want in your game then just set transform.position=worldPos. In this case, we want the object to move to the mouse position at a steady speed. Here is how to do it.

transform.position=Vector2.MoveTowards(transform.position,worldPos,moveSpeed*Time.deltaTime);

The above code will move the game object from current position to mouse position based on the speed specfied.

Here is the complete code for your reference

using UnityEngine;

public class MouseFollow : MonoBehaviour
{
    Vector3 pos;
    Vector3 worldPos;
    float moveSpeed=5;
    Vector3 distancefromMouse;

    void Update()
    {
        pos=Input.mousePosition;
        pos.z=Camera.main.nearClipPlane;
        worldPos=Camera.main.ScreenToWorldPoint(pos);
        distancefromMouse=worldPos-transform.position;   
        transform.rotation=Quaternion.LookRotation(distancefromMouse,Vector3.forward);        
        transform.position=Vector2.MoveTowards(transform.position,worldPos,moveSpeed*Time.deltaTime);
    }

}

Try it out and if you have any other question, feel free to leave a comment below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading