相当于“Transform.RotateAround”但非增量?精确角度

时间:2017-01-05 02:23:30

标签: c# unity3d unity5

我想围绕另一个对象旋转对象,就像“Transform.RotateAround”一样。 但不是增加每个调用的角度。 它应该将角度设置为精确的角度,就像使用“transform.localEulerAngles”

时一样

我现在一直在寻找年龄,但找不到一个好的,简洁的解决方案。

我已经发现了这个:(最好的“黑客”之一) http://answers.unity3d.com/questions/537155/is-there-a-method-to-set-the-exact-rotation-instea.html

它建议在每次“Transform.RotateAround”调用之前重置旋转,但不知何故这对我不起作用,这不是一个好的解决方案。

1 个答案:

答案 0 :(得分:1)

编辑:由于误解而修改的答案

围绕另一个对象B旋转一个对象A涉及两个步骤:

  1. 更新A
  2. 的位置
  3. 更新A的localRoation(以保持同一侧的B)。
  4. 这可以通过以下示例代码实现,其中属于脚本的变换围绕对象B中的rotationAxis旋转

    using UnityEngine;
    
    public class rot : MonoBehaviour {
        public GameObject reference;
        public float angle;
        public Vector3 rotationAxis;
        Vector3 direction;
        void Start () {
            // determine direction only once, to avoid infinite rotation.
            direction = transform.position - reference.transform.position;
        }
        void Update()
        {
            Quaternion rot = Quaternion.AngleAxis(angle, rotationAxis);
            transform.position = reference.transform.position + rot * direction;
            transform.localRotation = rot;
        }
    }