【发布时间】:2020-08-13 01:59:46
【问题描述】:
我制作了一个让玩家指向鼠标光标的脚本,但最近我发现了一个错误。当我将鼠标光标移动太多时(例如,当我将鼠标绕着对象旋转时,会导致对象四处移动。),对象最终会指向鼠标应该在的位置。例如,光标会向对象发出信号以查看它,并且对象最终看起来有点偏离,在快速操纵后感觉很奇怪。我怎样才能使对象始终面对光标,没有偏移,即使我尽可能地移动光标。
private void LateUpdate()
{
Vector3 lookAtPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 mousePoint = new Vector3(lookAtPoint.x, lookAtPoint.y, 0);
float angle = getAngle(transform.position, mousePoint);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 9f * Time.deltaTime);
float getAngle(Vector3 currentLocation, Vector3 mouseLocation)
{
float x = mouseLocation.x - currentLocation.x;
float y = mouseLocation.y - currentLocation.y;
return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x);
}
}
【问题讨论】:
标签: c# unity3d game-physics