• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by penguinburger1 · Sep 11, 2015 at 04:36 PM · scripting problemaxisconstraintsfreezepositionfreezerotation

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!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image penguinburger1 · Sep 11, 2015 at 11:13 PM 0
Share

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 REPLACE.

avatar image penguinburger1 penguinburger1 · Sep 11, 2015 at 11:14 PM 0
Share

alt text

problem1.png (292.9 kB)
avatar image penguinburger1 penguinburger1 · Sep 11, 2015 at 11:14 PM 0
Share

alt text

problem2.png (285.9 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

cannot have all constraints on in rigidbody? 1 Answer

How do i Invert Y axis in this Script? 2 Answers

Instantiated objects ignore most of the collisions. -1 Answers

holding position 1 Answer

I cannot freeze both position on one axis together with rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges