Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
6
Question by Alienjesus · Apr 23, 2011 at 05:24 PM · rigidbodyconstraintsfreezeposition

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?

Comment
Add comment
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

7 Replies

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

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

Comment
Add comment · Show 3 · 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
avatar image Alienjesus · Apr 23, 2011 at 06:09 PM 0
Share

I saw your answer first, so I'll accept this one. Thanks, I guess I just overlooked the obvious solution.

avatar image charnew · Feb 05, 2012 at 05:10 AM 0
Share

i tried this but how not to make the other object pass through it, it is like is$$anonymous$$inematic enabled

avatar image emmettbebop · Jul 25, 2018 at 07:53 PM 0
Share

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.

avatar image
6

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.

Comment
Add comment · Show 1 · 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
avatar image Joshua · Apr 23, 2011 at 05:32 PM 0
Share

Haha, 58 seconds earlier then me ;) I'm keeping $$anonymous$$e up because of the links though ^.^

avatar image
1

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; }

Comment
Add comment · Show 2 · 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
avatar image Alienjesus · Apr 23, 2011 at 06:06 PM 0
Share

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.

avatar image Teare · Apr 23, 2011 at 08:04 PM 0
Share

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.

avatar image
1

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; 
          
           
      }
  }  

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
avatar image
1

Answer by LarryTheBrave · Aug 18, 2019 at 05:27 AM

To unfreeze say .constraints = RigidbodyConstraints.None;

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
  • 1
  • 2
  • ›

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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