- Home /
Disabling rigidbody constraints in code - Freeze Position, Rotation etc
I'm trying to disable a rigidbody contraint to allow movement in the Y axis using javascript. I found the page showing how to do it in the script help files, but when I tried a very minor variation of it, it doesn't work, and I get an error. Here's my line of code:
transform.rigidbody.constraints = RigidbodyConstraints.FreezePositionY = false;
I also tried it like this:
transform.rigidbody.constraints = RigidbodyConstraints.FreezePosition = false;
and this:
transform.rigidbody.RigidbodyConstraints.FreezePosition = false;
None of those work, so simple question - How do I turn off the position freezing in code?
Answer by Joshua · Apr 23, 2011 at 05:30 PM
According to the docs this is how youd freeze the rotation around the x axis:
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
I haven't played around with it myself, but I admit it looks a little confusing. The way you set it is using these
I guess to stop rotation along the y axis you'd do
rigidbody.constraints = FreezePositionY
I saw your answer first, so I'll accept this one. Thanks, I guess I just overlooked the obvious solution.
i tried this but how not to make the other object pass through it, it is like is$$anonymous$$inematic enabled
That's not what the doc says. This is a different code example than the doc's example. This code will disable X and Y rotation.
Answer by Eric5h5 · Apr 23, 2011 at 05:29 PM
transform.rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
If you want to set multiple options without removing ones that are already set, you can use bitwise math. The docs show this.
Haha, 58 seconds earlier then me ;) I'm keeping $$anonymous$$e up because of the links though ^.^
Answer by Teare · Apr 23, 2011 at 05:47 PM
If none of these are working, you can always attach a script that forces the rotation to a value each FixedUpdate like this:
var angle : float;//
function Start (){ angle = transform.rotation.y; } function FixedUpdate (){ transfrom.rotation.y = angle; }
Thanks, but it's more that I want forces to start applying to it but only in certain directions. I'm sure this will help someone else down the line though.
Sorry I misread your question. You were asking about position specifically. This would also apply to position though, just change the transform.rotation to transform.position.
The direction in which forces apply will depend on the script from where they are applied. If you're talking about the physics engine though, rigidbody constraints ARE more useful since they only apply to the collisions.
Answer by shadowpuppet · Jun 05, 2015 at 08:23 AM
I got it to work, thanks all
var player : GameObject;
function OnTriggerEnter(other : Collider)
{
if(other.gameObject == player)
{
player.rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject == player)
{
player.rigidbody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionY;
}
}
Answer by LarryTheBrave · Aug 18, 2019 at 05:27 AM
To unfreeze say .constraints = RigidbodyConstraints.None;
Your answer
Follow this Question
Related Questions
holding position 1 Answer
JS - How do I change the constraints of an object? 1 Answer
RigidbobyConstraints2D is not working in Unity 5.0.2f1 Personal Edition!! 0 Answers
How do I query the RigidbodyConstraints? 3 Answers
Can you unfreeze a rigidbody.constraint position as you can freeze them? 2 Answers