• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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
4
Question by MrMelonPie · 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;

4 Replies

· Add your reply
  • Sort: 
avatar image
18
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 6 · 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 MrMelonPie · 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 MrMelonPie · Aug 21, 2017 at 08:09 PM 0
Share

UnityEngine.Component does not contain a definition for constraints

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

It Worked! :D

Tahnk you sooooo much! :D

avatar image MrMelonPie · Jul 14, 2014 at 09:28 AM 0
Share

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

avatar image robertbu · Jul 14, 2014 at 08:46 PM 1
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 MrMelonPie · Jul 15, 2014 at 08:27 AM 0
Share

Thanks a lot! :D

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

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

8 People are following this question.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

RigidbodyConstraints.FreezePositionY doesn't freeze the position 1 Answer

how can i make a List of rigidbody 1 Answer

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