- Home /
Freeze rigidbody position in script
How can I freeze a rigid body's position, when clicking and touching an object??
I just need the freeze part of it... I know you can use constraints, but I just don't understand how they work in a script!
Thanks.
rb.constraints = RigidbodyConstraints2D.FreezeAll;
Answer by robertbu · Jul 14, 2014 at 08:34 AM
Rigidbody constraints are handled by setting the appropriate bits. You can 'or' the bits together. So you can do something like:
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
A list of all the defined RigidbodyConstraints can be found here:
http://docs.unity3d.com/ScriptReference/RigidbodyConstraints.html
Another example, say you wanted the object to only move along the 'X' axis and rotate on the 'Y' axis. You would do:
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
Order does not matter.
Thanks a lot for your answer! :D
But i'm still not sure on how to do it properly...
rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
What would that do? :)
UnityEngine.Component does not contain a definition for constraints
If you want to unfreeze everything:
rigidbody.constraints = RigidbodyConstraints.None;
If you want to unfreeze just some things, then just assign the flags for what is to remain frozen.
btw, if you are using this in a 2D game, you have to type "RigidbodyConstraints2D" instead of just "RigidbodyConstraints"
Answer by cmz-neu4590 · Nov 29, 2017 at 04:30 PM
For others finding this if you want to freeze all constraints i think you can just do this... Rigidbody rb; rB.constraints = RigidbodyConstraints.FreezeAll; and then if you want to unfreeze rotacion i think you could do this rB.freezeRotation = false;
Answer by ABdoudj · Oct 06, 2018 at 04:30 PM
THIS WORKED PERFECT FOR ME i want my gameobject's collider set to trigger because i want the player to collect it using the ontriggerEnter2d function, but i still want this gameobject to fall to the ground once instantiated so i decided to freeze it's Rigidbody2D position when it reaches the level of the ground ;
public rigidbody2d BatteryRigid ;
if (transform.position.y<=(-3.2)) {
**BatteryRigid**.constraints = RigidbodyConstraints2D.FreezePosition;
}
Answer by SkyTheLimit · Sep 23, 2021 at 06:40 AM
This works. I Had to replace 'rigidbody' with 'rb' my rigidbody variable Now my code looks like this
rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
// And
rb.constraints = RigidbodyConstraints.None;
Thank you!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
cannot have all constraints on in rigidbody? 1 Answer
RigidbodyConstraints.FreezePositionY doesn't freeze the position 1 Answer
Multiple Cars not working 1 Answer