Мне кажется, я понимаю, что вы хотите.
Вы хотите иметь возможность щелкать где угодно, и объект должен следовать только за относительным движением мыши, а не за точной позицией, верно?
В противном случае вы можете просто использовать
transform.position = Vector2.Lerp(transform.position, secondPressPos, sensitivity * Time.deltaTime)
Но я не понимаю, почему вы делите currentSwipe
на 200
.. у вас, вероятно, есть свои причины.
В любом случае, как я понимаю, вы хотите, чтобы вы дополнительно хранили initialPos
позицию объекта в момент нажатия кнопки мыши. Затем вы добавите к этой исходной позиции currentSwipe
вместо того, чтобы использовать ее отдельно (= добавление к 0,0
)
float Speed = 50f;
float sensitivity = 5f;
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
private Vector2 initialPos;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
// store the current position
initialPos = transform.position;
}
// I would just make it exclusive else-if since you don't want to
// move in the same frame anyway
else if (Input.GetMouseButton(0))
{
secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = secondPressPos - firstPressPos;
if (firstPressPos != secondPressPos)
{
// Now use the initialPos + currentSwipe
transform.position = Vector2.Lerp(transform.position, initialPos + currentSwipe / 200, sensitivity * Time.deltaTime);
}
}
}
Обратите внимание , что в общем случае это зависит от ваших потребностей, но использование Lerp
на самом деле никогда не достигает точного положения ... оно только очень близко и очень медленно.