Your Cart
Loading
how to make object follow mouse unity unity 2d object follow mouse

How To Make Object Follow Mouse Unity

In this video tutorial, we’ll walk you through the steps to make an object follow the mouse cursor in Unity. You’ll learn how to use Unity’s scripting tools to track mouse position and update an object’s position accordingly. Whether you’re a beginner or looking to refresh your skills, this guide will give you a solid foundation for implementing smooth, interactive controls in your Unity projects. Grab your mouse and let’s dive in!



WATCH FULL TUTORIAL ON YOUTUBE



CHECK OUR PROJECTS


SUBSCRIBE FOR LATEST OFFERS



=========================================================


3 Unity Projects Only $70 - ON SALE!!!!


1. Water Sort 9000 Levels

https://payhip.com/b/cjDb8


2. Ball Sort Sort 9000 Levels

https://payhip.com/b/IbtoD


3. Sling Shot 250 Levels

https://payhip.com/b/PjLDH


Contact: unitycoderz2022@gmail.com


=========================================================



Script:


FollowMouse.cs


using UnityEngine;


public class FollowMouse : MonoBehaviour

{

  void Update()

  {

    // Get the mouse position in screen coordinates

    Vector3 mousePosition = Input.mousePosition;

     

    // Convert the mouse position from screen coordinates to world coordinates

    Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);

     

    // Set the z position to the object's current z position to keep it in 2D

    worldPosition.z = transform.position.z;

     

    // Update the object's position to follow the mouse

    transform.position = worldPosition;

  }

}





DOWNLOAD CODESTER FILES