我正在嘗試將游戲物件的位置凍結到 z 軸,但是當轉動游戲物件時,z 軸應該隨之轉動(或至少凍結位置)
所以基本上我希望我的物件只能向前、向后和上下移動,無論它面向什么方向。
剛體凍結位置與世界軸相關,而不是物件的旋轉軸。
感謝幫助,謝謝
物件的自上而下視圖,旋轉之前和之后
uj5u.com熱心網友回復:
您可以使用transform.forward. 如果這不是您想要的軸,請嘗試transform.right或transform.up。
目前還不清楚你在問什么。這里有兩個案例可以回答你的問題:
要將速度限制在“z 軸”:
//reference to the Rigidbody to restrict
private Rigidbody rig;
void Start(){
//find the Rigidbody attached to this GameObject
rig = GetComponent<Rigidbody>();
}
//Updates every physics frame
void FixedUpdate(){
//set the velocity to the component of the velocity that is parallel to the forward direction
rig.velocity = transform.forward * Vector3.Dot(transform.forward, rig.velocity);
}
如果您不想在“z 軸”上進行任何移動,請將該rig.velocity = ...行替換為rig.velocity -= ...
將此代碼放在附加到您的游戲物件的 MonoBehaviour(腳本)中。
請注意,如果游戲物件受到物理撞擊,它可能會旋轉,從而導致“z 軸”快速變化。為了防止這種情況,您可以使用剛體凍結旋轉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416070.html
標籤:
