一 目的
物体A旋转任意欧拉角,物体B要围绕该物体A旋转该欧拉角。
二 代码展示
public Transform t;
t.transform.RotateAround(transform.position, Vector3.forward,transform.eulerAngles.z);
t.transform.RotateAround(transform.position, Vector3.right, transform.eulerAngles.x);
t.transform.RotateAround(transform.position, Vector3.up, transform.eulerAngles.y);
三 理论分析
transform.RotateAround代表将变换围绕穿过世界坐标中的 point
的 axis
旋转 angle
度。
Unity中的旋转采取的是ZXY外旋的欧拉角系统,例如:
transform.Rotate(new Vector3(x, y, z),Space.Self);
与下方代码是等价的
transform.Rotate(new Vector3(0, 0, z), Space.Self);
transform.Rotate(new Vector3(x, 0, 0), Space.Self);
transform.Rotate(new Vector3(0, y, 0), Space.Self);
让物体B围绕物体A坐标先绕世界坐标系Z轴旋转,再围绕物体A坐标绕世界坐标系x轴旋转,
最好围绕物体A坐标绕世界坐标系Z轴旋转。