Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Bokaii · Jul 14, 2014 at 08:29 AM · c#rigidbodyfreezeposition

Freeze rigidbody position in script

How can I freeze a rigid body's position, when clicking and touching an object??

I just need the freeze part of it... I know you can use constraints, but I just don't understand how they work in a script!

Thanks.

Comment
Add comment · 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 Unicorn-slayer · Jan 07, 2016 at 11:28 AM 0
Share

rb.constraints = RigidbodyConstraints2D.FreezeAll;

5 Replies

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

Answer by robertbu · Jul 14, 2014 at 08:34 AM

Rigidbody constraints are handled by setting the appropriate bits. You can 'or' the bits together. So you can do something like:

 rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;

A list of all the defined RigidbodyConstraints can be found here:

http://docs.unity3d.com/ScriptReference/RigidbodyConstraints.html

Another example, say you wanted the object to only move along the 'X' axis and rotate on the 'Y' axis. You would do:

 rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;

Order does not matter.

Comment
Add comment · Show 9 · 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 Bokaii · Jul 14, 2014 at 09:14 AM 0
Share

Thanks a lot for your answer! :D

But i'm still not sure on how to do it properly...

 rigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;

What would that do? :)

avatar image vaniko2003 Bokaii · Aug 21, 2017 at 08:09 PM 0
Share

UnityEngine.Component does not contain a definition for constraints

avatar image Bokaii · Jul 14, 2014 at 09:27 AM 0
Share

It Worked! :D

Tahnk you sooooo much! :D

avatar image Bokaii · Jul 14, 2014 at 09:28 AM 1
Share

Another little question... how do I unfreeze again? :P

avatar image robertbu · Jul 14, 2014 at 08:46 PM 3
Share

If you want to unfreeze everything:

 rigidbody.constraints = RigidbodyConstraints.None;

If you want to unfreeze just some things, then just assign the flags for what is to remain frozen.

avatar image Bokaii · Jul 15, 2014 at 08:27 AM 0
Share

Thanks a lot! :D

avatar image ItsNoxL Bokaii · Dec 14, 2020 at 12:18 PM 0
Share

btw, if you are using this in a 2D game, you have to type "RigidbodyConstraints2D" instead of just "RigidbodyConstraints"

Show more comments
avatar image
1

Answer by cmz-neu4590 · Nov 29, 2017 at 04:30 PM

For others finding this if you want to freeze all constraints i think you can just do this... Rigidbody rb; rB.constraints = RigidbodyConstraints.FreezeAll; and then if you want to unfreeze rotacion i think you could do this rB.freezeRotation = false;

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
0

Answer by LeytonViner · Jun 18, 2017 at 10:17 AM

Is there any way of doing this in JavaScript?

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
0

Answer by ABdoudj · Oct 06, 2018 at 04:30 PM

THIS WORKED PERFECT FOR ME i want my gameobject's collider set to trigger because i want the player to collect it using the ontriggerEnter2d function, but i still want this gameobject to fall to the ground once instantiated so i decided to freeze it's Rigidbody2D position when it reaches the level of the ground ;

public rigidbody2d BatteryRigid ;

if (transform.position.y<=(-3.2)) {

         **BatteryRigid**.constraints = RigidbodyConstraints2D.FreezePosition;


     }

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
0

Answer by SkyTheLimit · Sep 23, 2021 at 06:40 AM

This works. I Had to replace 'rigidbody' with 'rb' my rigidbody variable Now my code looks like this

  rb.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
 
 // And
 
 rb.constraints = RigidbodyConstraints.None;

Thank you!

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.

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

I dont understand the syntax of RigidbodyConstraints2D and freezeposition, I keep getting an error C# 1 Answer

Distribute terrain in zones 3 Answers

cannot have all constraints on in rigidbody? 1 Answer

RigidbodyConstraints.FreezePositionY doesn't freeze the position 1 Answer

Multiple Cars not working 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