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
3
Question by Nanofus · Feb 22, 2013 at 06:44 PM · rotationpositionworld spaceconstraintlocal space

Rigidbody constraints in local space?

The position and rotation constraints you can set for rigidbodies are in world space. Is there a way to constrain a rigidbody's position and rotation in local space? I couldn't find an answer with Google, just a lot of other people asking the same question.

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
9
Best Answer

Answer by OhNoDinos · Feb 16, 2014 at 12:42 AM

I realize this question is old, but I recently solved this problem myself, if any other people come stumbling across this thread.

Khada's response is correct, but in order to actually achieve this, you can use the following code (C#).

 Vector3 localVelocity = transform.InverseTransformDirection(rigidbody.velocity);
 localVelocity.x = 0;
 localVelocity.z = 0;
         
 rigidbody.velocity = transform.TransformDirection(localVelocity);

This translates the rigidbody's velocity into local space, constrains the values you need, then translates it back. This particular code constrains x and z movement in local space.

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 alonsoGarrote · Mar 13, 2014 at 05:53 AM 0
Share

Thank you OhNoDinos, I was looking for this also. Do you know if its also possible to do the same with rigidbody.position?, so The rigidbody stays at the same height, Regards.

avatar image backer_s · Jun 29, 2017 at 04:49 PM 0
Share

Great job!! I was struggling with this, and it worked for me just fine! :)

avatar image loliconest · Mar 12, 2018 at 06:23 PM 0
Share

Hello, I wonder if this works if the rigidbody is pushed by a kinematic rigidbody.

Basically, I want to make a button, and there is a kinematic rigidbody collider attached to the player's hand. The button has a rigidbody and a spring joint.

If I freeze the rigidbody constraints, the button won't be pushed by the hand along any of the global axes I froze. However, if I don't tick the rigidbody constraints but only use your code or use other kinematic rigidbody colliders on the button's sides to prevent it from moving to unwanted positions, the body will just keep jittering. And if I push it over the colliders that blocks the button, the button will just remain on the other side of the collider.

avatar image
3

Answer by jeyb · Jul 19, 2016 at 08:04 PM

Just in case anyone comes across this old question, in Unity 5 rigidbody constraints are applied in local space: https://docs.unity3d.com/ScriptReference/Rigidbody-constraints.html

Comment
Add comment · Show 4 · 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 Naphier · Jan 03, 2017 at 12:21 AM 1
Share

Although the docs say this, I think it's a mistake because they still lock in world for me on 5.5...

avatar image michael_house · Feb 16, 2017 at 05:47 AM 2
Share

Only rotation is local, position is global.

avatar image backer_s · Jun 29, 2017 at 03:50 PM 0
Share

it's written that: position constraints are applied in World space, and rotation constraints are applied in Local space.

avatar image kamran-bigdely · Jul 13, 2017 at 01:53 AM 0
Share

Wrong. "Freeze Position" property still works in world space not local space.

avatar image
2

Answer by Khada · Feb 22, 2013 at 06:48 PM

In short, no. Rigid bodies don't do calculations in local space and in general an object with a rigid body shouldn't be the child of another object.

What you can do is constrain the axes that you want to behave differently and code the behavior yourself.

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 Nanofus · Feb 22, 2013 at 07:48 PM 0
Share

Thanks a lot for the answer.

avatar image
2

Answer by kamran-bigdely · Jul 13, 2017 at 10:49 PM

I noticed that setting local position to zero was effective to constrain a rigidbody's position along specific axis :

 public class FreezeLocalAxes : MonoBehaviour {
     private Rigidbody _rigidbody;
     private float _localX = 0;
     private float _localY = 0;
     private float _localZ = 0;
     public bool _freezeAlongX = false;
     public bool _freezeAlongY = false;
     public bool _freezeAlongZ = false;
     // Use this for initialization
     void Start () {
         _rigidbody = gameObject.GetComponent<Rigidbody>();
 
     }
 
 void Update () {
             _localX = transform.localPosition.x;
             _localY = transform.localPosition.y;
             _localZ = transform.localPosition.z;
             
             if (_freezeAlongX) _localX = 0;
             if (_freezeAlongY) _localY = 0;
             if (_freezeAlongZ) _localZ = 0;
             gameObject.transform.localPosition = new Vector3(_localX, _localY, _localZ);
     }
 }

If you want to freeze the rotation along a specific axis, just mark them in Rigidbody component because they are already in local coordinates (in contrast with 'freeze position' which is in world coordinate system).

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 swalex · Mar 06, 2016 at 01:55 PM

You can attach following behaviour script to your rigidbody game object to keep it always at a plane or beam cutting / pointing at a target transform:

 using System;
 using UnityEngine;
 
 [RequireComponent( typeof( Rigidbody ) )]
 public class CameraRail : MonoBehaviour
 {
     [Flags]
     public enum LockFlags
     {
         None,
         Horizontal,
         Vertical,
         Both
     }
     
     [Range( 0, 25 )]
     public float Force = 1;
     
     public LockFlags Lock = LockFlags.Both;
     
     public Transform Target;
     
     private Rigidbody _rigidbody;
     
     private Transform _transform;
     
     public void Awake()
     {
         _rigidbody = GetComponent<Rigidbody>();
         _transform = GetComponent<Transform>();
     }
     
     public void Update()
     {
         if ( Lock == LockFlags.None || Force <= 0 ) return;
         
         bool lockX = ( Lock & LockFlags.Horizontal ) == LockFlags.Horizontal;
         bool lockY = ( Lock & LockFlags.Vertical ) == LockFlags.Vertical;
         
         Vector3 velocity = _transform.InverseTransformDirection( _rigidbody.velocity );
         if ( lockX ) velocity.x = 0;
         if ( lockY ) velocity.y = 0;
         _rigidbody.velocity = _transform.TransformDirection( velocity );
         
         Vector3 distance = Target.position - _transform.position;
         Vector3 idealPosition = Target.position - _transform.forward * distance.magnitude;
         Vector3 correction = idealPosition - _transform.position;
         
         correction = _transform.InverseTransformDirection( correction );
         if ( !lockX ) correction.x = 0;
         if ( !lockY ) correction.y = 0;
         correction.z = 0;
         correction = _transform.TransformDirection( correction );
         
         _rigidbody.velocity += correction * Force;
     }
 }
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

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

Related Questions

Rotate and transform in local space 2 Answers

Objects not always Spawning At Correct Location 2 Answers

how to ignore transform.position.y 3 Answers

Prefab Character position/rotation resets 0 Answers

Objects Instantiating at wrong position 3 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