using System.Collections; using System.Collections.Generic; using UnityEngine; public class MousePosition : MonoBehaviour { [SerializeField] private Camera mainCamera; void Update() { // Position of the mouse Vector3 mousePosScreen = Input.mousePosition; // Convert the 2D mouse position to a 3D world position Vector3 mousePosWorld = mainCamera.ScreenToWorldPoint(new Vector3(mousePosScreen.x, mousePosScreen.y, Mathf.Abs(mainCamera.transform.position.z - transform.position.z))); // Set the x and y Vector3 newPos = transform.position; newPos.x = mousePosWorld.x; newPos.y = mousePosWorld.y; transform.position = newPos; } }