Reputation: 305
In the Unity inspector (v5+), you have a "Freeze Z" rotation, any idea which is the equivalent in code? The idea is to enable/disable Z rotation of a 2D object due a certain condition by C# code.
Upvotes: 4
Views: 7378
Reputation: 7100
To put this as an answer for 2D, use the following:
Rigidbody2D body2d;
void Awake() {
body2d = GetComponent<Rigidbody2D> ();
}
void Update () {
// on this line, calculate condition for freezing rotation
if (condition) {
body2d.constraints = RigidbodyConstraints2D.FreezeRotation;
} else {
body2d.constraints = RigidbodyConstraints2D.None;
}
}
Upvotes: 0
Reputation: 8813
You want to set the Rigidbody.constraints to a RigidbodyConstraint
:
transform.rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
The RigidbodyConstraint
constants can be combined with the |
operator:
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
Upvotes: 7