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
=========================================================
3 Unity Projects Only $70 - ON SALE!!!!
1. Water Sort 9000 Levels
2. Ball Sort Sort 9000 Levels
3. Sling Shot 250 Levels
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;
}
}