Version: 2021.2
public RigidbodyConstraints constraints ;

描述

控制该刚体的模拟自由度。

默认情况下,该属性设置为 RigidbodyConstraints.None - 允许沿所有轴旋转和移动。 在某些情况下,您可能需要限制 Rigidbody 只能沿某些轴移动或旋转, 例如在开发 2D 游戏时。可以使用按位 OR 运算符组合多个 约束。

Note that position constraints are applied in World space, and rotation constraints are applied in the inertia space (relative to 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; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961