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!


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:

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.

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;

Is there any way of doing this in JavaScript?

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;


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!