• 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 /
This question was closed Aug 09, 2018 at 09:17 AM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
6
Question by CaptainIcy281 · Apr 12, 2012 at 02:50 AM · rigidbodyconstraintsfreezeposition

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!

Comment
Comments Locked · Show 1
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 GC1983 · Apr 12, 2012 at 03:08 AM 0
Share

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.

2 Replies

  • Sort: 
avatar image
36
Best Answer

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;
Comment
Comments Locked · Show 8 · 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 CaptainIcy281 · Apr 12, 2012 at 03:24 AM 0
Share

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.

avatar image Eric5h5 · Apr 12, 2012 at 03:37 AM 1
Share

He already explained the simple way to just use FreezePositionY. The constraints are bitmasks, so you turn on/off the bits you want.

avatar image CaptainIcy281 · Apr 12, 2012 at 03:40 AM 0
Share

Right, yeah, sorry. That works fine. Thanks, you two. ;)

avatar image harschell · Oct 26, 2012 at 05:46 AM 0
Share

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

avatar image aldonaletto · Oct 27, 2012 at 01:35 AM 0
Share

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.

Show more comments
avatar image
6

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;
 }
Comment
Comments Locked · 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

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

11 People are following this question.

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

Related Questions

How do I query the RigidbodyConstraints? 3 Answers

holding position 1 Answer

cannot have all constraints on in rigidbody? 1 Answer

Disabling rigidbody constraints in code - Freeze Position, Rotation etc 7 Answers

JS - How do I change the constraints of an object? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges