public class NewBehaviourScript : MonoBehaviour
{
private Vector3 sc;
private Vector3 offsey;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnMouseDown()
{ sc = Camera.main.WorldToScreenPoint(transform.position);//获取物体的屏幕坐标
offsey = sc - Input.mousePosition;//获取物体与鼠标在屏幕上的偏移量
}
void OnMouseDrag()
{
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + offsey);//将拖拽后的物体屏幕上坐标还原
}
}
unity中通过鼠标来拖拽物体
最新推荐文章于 2024-01-09 16:18:55 发布