- Home /
The question is answered, right answer was accepted
Can you unfreeze a rigidbody.constraint position as you can freeze them?
Yes, a simple question from me!
I want to freeze the rigidbody's Y position when the game is paused, as in my script:
if(GameManager.gamePaused == true) { transform.rigidbody.constraints = RigidbodyConstraints.FreezePositionY; }
And of course, when I unpause it I naturally want to unfreeze it. Is there a simple way to do that? I've looked around the various tutorials and Unity's scripting API pages, can't find anything about it.
Thanks!
I tested what you had and the only thing I could find was using RigidbodyConstraints.None;. This will turn all constraints off though. So if you dont have any of the other constraints on, then this will work perfectly for you. Otherwise, it looks that you can individually turn the other constraint on that you need that were turned off by that call.
Answer by aldonaletto · Apr 12, 2012 at 03:21 AM
RigidbodyConstraints are bit masks, thus you can use + or | (bitwise OR) to set several constraints at once. To disable a single constraint, AND rigidbody.constraints with the negated mask (~):
rigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
But if you don't have other constraints active, just set rigidbody.constraints to RigidbodyConstraints.None:
rigidbody.constraints = RigidbodyConstraints.None;
But see, I have all rigidbody Rotations disabled in the Inspector, so using Rigidbody.Constraints.None enables the rotation on unpause. I was hoping there was a simple way to just enable position.Y
I can use some other method I suppose, hmm.
He already explained the simple way to just use FreezePositionY. The constraints are bitmasks, so you turn on/off the bits you want.
Right, yeah, sorry. That works fine. Thanks, you two. ;)
hi can you tell me why this is not working whats Wrong here???? void getInput() { if (Input.GetButtonDown("Jump")) { rigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY; flipGravity(); Debug.Log("gravity Flipped"); } }
This code isn't enough to know what is going on. You should post a new question with the relevant part of your code: where rigidbody.constraints are set, show the function flipGravity() etc.
Answer by gilley033 · Oct 15, 2013 at 08:31 PM
Another way to do this is to simply save the constraints in Awake or Start before changing them.
var originalConstraints : RigidbodyConstraints;
function Awake()
{
originalConstraints = rigidbody.constraints;
}
function FreezeConstraints()
{
rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
}
function UnFreezeConstraints()
{
rigidbody.constraints = originalConstraints;
}
Follow this Question
Related Questions
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
Disabling rigidbody constraints in code - Freeze Position, Rotation etc 7 Answers
How do I query the RigidbodyConstraints? 3 Answers
Attempt to disable constraints using RigidbodyConstraints.None has no effect 0 Answers