在Unity中获取另一个对象的旋转值



我有一个相机,它连接在胶囊上,并独立旋转。

但是我需要胶囊沿着Y轴跟随摄像机旋转。如何获得相机的y轴旋转值?试过transform. rotate .y. 但是它给出了一个错误。

Transform.rotation返回一个Quaternion(参见维基百科-四元数)-它不是3而是4组分xyzw

!=比;除非你确切地知道你在做什么——这几乎从来都不是这种情况^^——否则你永远不会想直接触摸它的任何组件。

不幸的是,Transform.eulerAngles对于您的用例也不是真正可靠的。


所以我要做的是依靠Vector3代替。

// take camera's forward vector
private forward = yourCamera.transform.forward;
// erase the Y axis => vector is now only on the XZ plane rotating around Y
forward.y = 0;
// rotate the capsule so its forward vector aligns with "forward"
yourCapsule.transform.rotation = Quaternion.LookRotation(forward);
// could also do
//yourCapsule.transform.forward = forward;

参见Quaternion.LookRotation

最新更新