我正在点击并点击汽车运动,但我可以做动作只能点击停车。当我点击任何一点时,我想要移动汽车。我写了这段代码。还可以顺利旋转吗?因为有车的滑行标记,当我转动车时,它突然转向你能帮助我吗?
void Update()
{
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
marker.position = mousePosition;
if (Input.GetMouseButton(0))
{
moveToPosition = true;
}
else if (Input.GetMouseButtonUp(0))
{
moveToPosition = false;
SoundManagerScript.stopCarAudioSource.Play();
SoundManagerScript.loopEngineAudioSource.Stop();
}
if (moveToPosition)
{
direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
if (Vector3.Distance(transform.position, mousePosition) < 100f)
{
moveToPosition = false;
}
transform.up = direction * moveSpeed;
}
}
答案 0 :(得分:1)
目前,您只有代码进入OnMouseDrag
功能,并且您错误地使用了Vector.Lerp
。看here,但长话短说,lerp需要通过Update
或IEnumerable
不断更新。
举一个非常简单的例子,我建议尝试这样的事情;
//get your click to register to this function
void OnMouseClick()
{
moveToPosition = true;
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
}
void Update()
{
if(moveToPosition)
{
increase += Time.deltaTime;
direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
Vector3 newDir = Vector3.RotateTowards(transform.forward, direction, step, 0.0F);
transform.rotation = Quaternion.LookRotation(newDir);
if(Vector3.Distance(transform.position, mousePosition) < 5f)
{
moveToPosition = false;
}
}
}
它未经测试,但基本足以让你继续看到移动。