我有一个附加到对象的脚本,该脚本必须将对象与鼠标一起移动。就像我单击对象一样,它会重置为鼠标的位置,并且不考虑偏移量。
private void Update()
{
Vector3 moveTo = EDUCam.Instance.attachedCamera.ScreenToWorldPoint(EDUCam.Instance.mousePosition);
moveTo.z = 0f;
if (canDrag)
{
if (Input.GetMouseButtonDown(0))
offset = transform.position - moveTo;
if (horizontal)
{
moveTo.x = Mathf.Clamp(moveTo.x, 0f, 5.5f);
//transform.localPosition = new Vector3(moveTo.x, transform.localPosition.y) + offset;
gameObject.GetComponent<Rigidbody2D>().MovePosition(new Vector2(moveTo.x + offset.x, transform.position.y));
}
}
}