How to Make an Object Follow the Mouse Cursor in Unity

Unity is a powerful game development platform that offers endless possibilities for creating interactive experiences. One common feature in many games is the ability for objects to follow the mouse cursor. Whether you're developing a 2D or 3D game, implementing this functionality can add a level of interactivity and immersion for your players. In this tutorial, we'll guide you through the process of making an object follow the mouse cursor in Unity using C# scripting.

Steps

  • Create a New Unity Project First, launch Unity, and create a new project. Choose either a 2D or 3D project template depending on your game's requirements.
  • Prepare the Object to Follow the Cursor Before writing any code, you'll need an object in your scene that will follow the mouse cursor. This can be any sprite or game object you want to control.
  • Attach a Script to the Object Select the object you want to follow the mouse cursor in the Unity hierarchy. Then, create a new C# script and attach it to the selected object.
  • Write the C# Script Open the script and write the following code:

'FollowCursor.cs'

using UnityEngine;

public class FollowCursor : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        // Get the current mouse position in screen coordinates
        Vector3 mousePosition = Input.mousePosition;

        // Convert the mouse position from screen space to world space
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        // Update the object's position to the mouse position
        transform.position = new Vector3(mousePosition.x, mousePosition.y, transform.position.z);
    }
}

Explanation of the Code:

  • The 'Update()' method is called every frame.
  • 'Input.mousePosition' gets the current mouse position in screen coordinates.
  • 'Camera.main.ScreenToWorldPoint()' converts the mouse position from screen space to world space, considering the camera's perspective.
  • Finally, we update the object's position to the converted mouse position.

Save the script and return to Unity. Unity will automatically compile the script.

Testing

  • Test the Scene Press the play button in Unity and move your mouse around the game window. You should see the object following the cursor.
  • Adjustments (Optional) You can adjust the object's behavior by modifying the script. For example, you could add a smoothing effect to make the movement less abrupt.

Further Customization

  • Add constraints to the object's movement, such as clamping its position within certain bounds.
  • Change the object's appearance or add additional functionality as needed.

Save Your Work Once you're satisfied with the behavior, save your Unity project to retain your progress.

Conclusion

You've successfully created a script to make an object follow the mouse cursor in Unity. This simple yet effective feature can enhance the interactivity of your games and create a more immersive experience for players. Feel free to experiment further and customize the behavior to suit your project's unique requirements.

Suggested Articles
Making Inventory and Item Crafting System in Unity
Creating a Simple 2D Bullet System in Unity
Display Text on Object Touch in Unity
Unity How to Create a Shader
How to Become a Better Programmer in Unity
Coding a Simple Inventory System With UI Drag and Drop in Unity
Implementing Object Pooling in Unity