控制此刚体模拟允许的自由度。
默认情况下,此设置为RigidbodyConstraints.None,允许绕所有轴旋转和平移。在某些情况下,您可能希望将Rigidbody约束为仅沿某些轴移动或旋转,例如在开发 2D 游戏时。您可以使用按位或运算符组合多个约束。
请注意,位置约束在世界空间中应用,旋转约束在惯性空间中应用(相对于Rigidbody.inertiaTensorRotation)。
//Attach this script to a GameObject. //Attach a Rigidbody to the GameObject by clicking the GameObject in the Hierarchy and //clicking the Add Component button. Search for Rigidbody in the field and select //it when shown.
using UnityEngine;
public class Example : MonoBehaviour { Rigidbody m_Rigidbody;
void Start() { m_Rigidbody = GetComponent<Rigidbody>(); //This locks the RigidBody so that it does not move or rotate in the Z axis. m_Rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationZ; } }