【问题标题】:Unity3d rotate arrow towards mouse positionUnity3d 向鼠标位置旋转箭头
【发布时间】:2014-10-07 08:53:51
【问题描述】:

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

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。这不是在这里提问的好方法。到目前为止,您是否尝试过任何方法来解决您的问题?首先展示你的努力,这样人们就可以展示他们的努力。请先阅读FAQHow to Askhelp center
  • 这不是我第一次发帖。我故意这样做是为了看到不同的想法。我不想展示我的解决方案,所以人们不会那样做。

标签: unity3d rotation gameobject


【解决方案1】:

我刚刚回答了一个非常相似的问题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。从这些示例中修改代码以执行您想要的操作应该很简单。

【讨论】:

    【解决方案2】:

    这一切都取决于你的箭头是如何设置的,你可以使用 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

    快乐编码。

    【讨论】:

    • 箭头是一个精灵。它在 0,0 中。枢轴是箭头的底部,它指向顶部。它应该看起来像模拟时钟,箭头应该像时钟指针一样旋转,指向鼠标。我正在使用它,但它不工作,正面朝鼠标旋转float lookFactor = 0.5f; float distance = (transform.position.z - Camera.main.transform.position.z) * lookFactor; Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance); position = Camera.main.ScreenToWorldPoint(position); transform.LookAt(position);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    相关资源
    最近更新 更多