【发布时间】:2019-04-16 02:33:24
【问题描述】:
我制作了下面的脚本,用鼠标位置平滑移动对象,它运行顺利,但我在这个脚本中有一个问题,当我点击并移动鼠标时,对象立即移动到 (0, 0, 0) 位置然后它开始随着鼠标的移动而移动。
我该如何解决这个问题,使对象从最后一个位置移动而不是从 (0, 0, 0) 位置移动?
脚本:
float Speed = 50f;
float sensitivity = 5f;
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButton(0))
{
secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
if (firstPressPos != secondPressPos)
{
transform.position = Vector2.Lerp(transform.position, new Vector3(currentSwipe.x / 200, currentSwipe.y / 200, transform.position.z), sensitivity * Time.deltaTime);
}
}
}
【问题讨论】: