- Home /
Turn on Specific Axis Constraints - Keeping Others Selected (JS)
I have a button that turns axis constraints positions X and Z on and off. Here's the script (JavaScript):
var toggle = false;
function Toggle () {
toggle = !toggle;
if (toggle) {
GetComponent.<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
}else{
GetComponent.<Rigidbody>().constraints &= ~RigidbodyConstraints.FreezePositionX;
GetComponent.<Rigidbody>().constraints &= ~RigidbodyConstraints.FreezePositionZ;
}
}
If I turn it on, and other constraints are selected, it turns them off and keeps X and Z on. If I turn it off, and other constraints are selected, only X and Z turn off - like it's supposed to.
if statement isn't working the way I want it to. else statement is working correctly.
I don't understand what I need to do to turn on X and Z while other constraints are on WITHOUT turning them off.
(I've looked all over the internet for this, and I can't find a proper solution. I did find this, which helped me with the else statement, but I don't understand the logic enough to apply it to the if statement: " http://answers.unity3d.com/questions/238887/can-you-unfreeze-a-rigidbodyconstraint-position-as.html ").
I'd really appreciate someone to point out my mistake and help me understand this logic. Thank you so much in advance!
I think the if statement is saying that constraints HAVE to equal position X and Z, nothing else. So every time the toggle is true, it resets constraints to what's written. The else section is ADDING to the value. If section is REPLACING the value. I just don't know how to get the if section to ADD to the value ins$$anonymous$$d of REPLAC$$anonymous$$
Answer by penguinburger1 · Sep 20, 2015 at 05:43 PM
function Toggle () {
toggle = !toggle;
if (toggle) {
GetComponent.<Rigidbody>().constraints |= RigidbodyConstraints.FreezePositionX;
GetComponent.<Rigidbody>().constraints |= RigidbodyConstraints.FreezePositionZ;
}else{
GetComponent.<Rigidbody>().constraints &= ~RigidbodyConstraints.FreezePositionX;
GetComponent.<Rigidbody>().constraints &= ~RigidbodyConstraints.FreezePositionZ;
}
}
The main bit being the |= on the if statement.
Your answer
Follow this Question
Related Questions
Attempt to disable constraints using RigidbodyConstraints.None has no effect 0 Answers
cannot have all constraints on in rigidbody? 1 Answer
How do i Invert Y axis in this Script? 2 Answers
How to freeze position x and z on rigidbody(both!) 2 Answers
Instantiated objects ignore most of the collisions. -1 Answers