using System.Collections; using System.Collections.Generic; using UnityEngine; public class PinPreciseDrag : MonoBehaviour { Vector3 target_Screen; Vector3 target_World; Vector3 mouse_Screen; Vector3 mouse_World; Vector3 offset; bool IsClickTarget; private void Start() { IsClickTarget = false; target_Screen = Camera.main.WorldToScreenPoint(transform.position); } void Update() { if (Input.GetMouseButton(0) && IsClickTarget == true) { mouse_Screen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, target_Screen.z); mouse_World = Camera.main.ScreenToWorldPoint(mouse_Screen); transform.position = mouse_World + offset; } } void OnMouseDown() { IsClickTarget = true; mouse_Screen = new Vector3(Input.mousePosition.x, Input.mousePosition.y, target_Screen.z); mouse_World = Camera.main.ScreenToWorldPoint(mouse_Screen); offset = transform.position - mouse_World; //目标物体世界位置 减去 鼠标世界位置 } }