Unity3d将箭头指向鼠标位置

时间:2014-10-07 08:53:52

标签: unity3d rotation gameobject

在团结中,我在箭头的开头有一个带有枢轴的箭头精灵。

现在我需要在2d旋转该箭头,使其始终指向鼠标。
需要使用lerpslerp来完成,所以当我将鼠标移动太快时,它不会跳到那里但会慢慢滑动到那个位置。

2 个答案:

答案 0 :(得分:0)

这一切都取决于您的箭头设置方式,您可以使用rotateTowards吗?

如果是这样,您可以将ScreenToWorldPoint与rotateTowards一起使用

(ps,因为我在工作时没有测试功能:P)

void Update() {
    transform.rotateTowards(camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));
}

对于lerp函数,您应该是Check here for speed variable stuff

快乐编码。

答案 1 :(得分:0)

我刚回答了一个非常相似的问题here。为简单起见,以下代码是example from the Unity forum,您可以使用它来围绕z轴旋转对象。

Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

Here is the Unity3D documentation for slerphere is the documentation for lerp。应该直接修改代码,以便从这些示例中执行您想要的操作。

相关问题