using UnityEngine; using UnityEngine.UI; public class MoveByMouse2D : MonoBehaviour { public GameObject obj; public float speed = 1.5f; Vector3 target; void Start() { target = transform.position; } void Update() { if (Input.GetMouseButtonDown(0)) { target = Camera.main.ScreenToWorldPoint(Input.mousePosition); target.z = transform.position.z; Vector2 direction = new Vector2(target.x - transform.position.x, target.y - transform.position.y); obj.transform.up = direction; } transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); } }