我試圖在遊戲對象的樞軸上旋轉設定的角度。這個樞軸是傾斜的,有點歪斜。當我用下面的代碼設置角度時,它根據世界Y軸旋轉,我如何在傾斜的遊戲對象的軸心上進行旋轉?對象樞軸Y軸上的旋轉[UNITY]
steer.transform.rotation = Quaternion.AngleAxis(angle, new Vector3(0 ,1, 0));
在此先感謝
我試圖在遊戲對象的樞軸上旋轉設定的角度。這個樞軸是傾斜的,有點歪斜。當我用下面的代碼設置角度時,它根據世界Y軸旋轉,我如何在傾斜的遊戲對象的軸心上進行旋轉?對象樞軸Y軸上的旋轉[UNITY]
steer.transform.rotation = Quaternion.AngleAxis(angle, new Vector3(0 ,1, 0));
在此先感謝
new Vector3(0,1,0)
是 「世界空間」 上(同Vector3.up BTW)。去的「局部空間」起來最簡單的方法是使用transform.up
旋轉對象繞軸心,用Transform.RotateAround()方法。所以代碼看起來像這樣:
Vector3 pivot = ...; // Object's pivot in world coordinates
float angle = ...; // You also want to multiply angle by Time.deltaTime
// To rotate around the world's up axis
steer.transform.RotateAround(pivot, Vector3.up, angle);
// To rotate around the object's up axis
steer.transform.RotateAround(pivot, steer.transform.up, angle);