Unity3Dでリジッドボディの位置と回転をフリーズするための注意事項



Precautions Freezing Position



Unity3Dでリジッドボディの位置と回転をフリーズするための注意事項

1.プログラムを使用して、剛体の位置をフリーズします

using System.Collections using System.Collections.Generic using UnityEngine public class pengZhuang : MonoBehaviour{ //Start is called before the first frame update private Rigidbody rigidbody void Start(){ rigidbody = this.GetComponent<Rigidbody>() //This script hangs on the object to which this rigid body belongs, and obtains the rigid body component } void Update(){ rigidbody.constraints = RigidbodyConstraints.FreezePositionY //Freeze the movement of the rigid body on the Y axis, the object can only move on the plane composed of the X axis and the Z axis } }

効果画像1: 画像

2.プログラムを使用して、剛体の回転をフリーズします

using System.Collections using System.Collections.Generic using UnityEngine public class pengZhuang : MonoBehaviour{ //Start is called before the first frame update private Rigidbody rigidbody void Start(){ rigidbody = this.GetComponent<Rigidbody>() //This script hangs on the object to which this rigid body belongs, and obtains the rigid body component } void Update(){ rigidbody.constraints = RigidbodyConstraints.FreezeRotationY //Freeze the rotation of the rigid body around the Y axis, the object can only rotate around the X axis and Z axis } }

効果画像2:



3.リジッドボディの位置と回転を同時にフリーズします。

using System.Collections using System.Collections.Generic using UnityEngine public class pengZhuang : MonoBehaviour{ //Start is called before the first frame update private Rigidbody rigidbody void Start(){ rigidbody = this.GetComponent<Rigidbody>() //This script hangs on the object to which this rigid body belongs, and obtains the rigid body component } void Update() RigidbodyConstraints.FreezeRotationY //Freeze the movement of the rigid body on the Y axis, and freeze the rotation of the rigid body around the Y axis }

効果画像3:

4.注意が必要な事項

using System.Collections using System.Collections.Generic using UnityEngine public class pengZhuang : MonoBehaviour{ //Start is called before the first frame update private Rigidbody rigidbody void Start(){ rigidbody = this.GetComponent<Rigidbody>() //This script hangs on the object to which this rigid body belongs, and obtains the rigid body component } void Update(){ rigidbody.constraints = RigidbodyConstraints.FreezePositionX rigidbody.constraints = RigidbodyConstraints.FreezePositionY rigidbody.constraints = RigidbodyConstraints.FreezePositionZ rigidbody.constraints = RigidbodyConstraints.FreezeRotationX rigidbody.constraints = RigidbodyConstraints.FreezeRotationY rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ //The result of running this code is just freezing around the Z axis } }

複数の軸を同時にフリーズする場合は、次のようにすることはできませんのでご注意ください。



rigidbody.constraints = RigidbodyConstraints.FreezePositionX rigidbody.constraints = RigidbodyConstraints.FreezePositionY rigidbody.constraints = RigidbodyConstraints.FreezePositionZ rigidbody.constraints = RigidbodyConstraints.FreezeRotationX rigidbody.constraints = RigidbodyConstraints.FreezeRotationY rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ

後者のプログラムは前のプログラムを上書きし、実行の最終的な効果は、Z軸を中心に回転する剛体をフリーズすることだけです。

効果画像4:

5.例(オブジェクトが着地して地面に衝突した後、Y軸でのオブジェクトの移動と、Z軸とX軸を中心とした回転がフリーズし、オブジェクトが水平面で移動および回転できるようになります)

using System.Collections using System.Collections.Generic using UnityEngine public class pengZhuang : MonoBehaviour { // Start is called before the first frame update private Rigidbody rigidbody void Start() { rigidbody = this.GetComponent<Rigidbody>() } private void OnCollisionEnter(Collision collision) rigidbody.constraints = RigidbodyConstraints.FreezePositionY }

効果画像5:



6.特定の軸を除く他のすべての軸をフリーズします

using System.Collections using System.Collections.Generic using UnityEngine public class pengZhuang : MonoBehaviour{ //Start is called before the first frame update private Rigidbody rigidbody void Start(){ rigidbody = this.GetComponent<Rigidbody>() //This script hangs on the object to which this rigid body belongs, and obtains the rigid body component } void Update(){ rigidbody.constraints = ~RigidbodyConstraints.FreezePositionY //The effect is shown in the figure below } }

効果画像6:

何か欠点があれば、メッセージを残してお互いを宣伝してください、ありがとう! ! !