等价于 "Transform.RotateAround" 但非增量 ??精确角度

An equivalent for "Transform.RotateAround" BUT NON incremental ?? exact angle

我想像 "Transform.RotateAround" 一样围绕另一个对象旋转一个对象。 但不是增加每次调用的角度。 它应该像使用 "transform.localEulerAngles"

时一样将角度设置为准确的角度

我已经搜索了很多年,但找不到好的、干净且简单的解决方案。

我已经找到了这个:(最好的之一 "hacks") http://answers.unity3d.com/questions/537155/is-there-a-method-to-set-the-exact-rotation-instea.html

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

编辑:由于误解修改了答案

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

  1. 更新 A 的位置
  2. 更新 A 的 localRoation(以保持用同一侧看 B)。

这可以通过以下示例代码实现,其中属于脚本的变换围绕对象 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;
    }
}