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
0
Question by BurningKrome · Mar 08, 2015 at 08:57 AM · configurablejointlocked

ConfigurableJoint Object snaps back to old position when AngularMotion locked

I'm working in Unity3D version 4.

--- In a Nutshell---

  1. Two objects.

  2. A ConfigurableJoint is created between them with Motionx/y/z and angularMotionx/y/z locked so their relative positions to each other doesn't change.

  3. OnMouseDrag changes angularMotionx/y/z to "Free"

  4. User drags one object around the other.

  5. When user releases mouse, I simply set the ConfigurableJoint's angularMotionx/y/z BACK to locked.

DESIRED OUTCOME: The objects stay in their new positions, and the configged joint is again locked so the two objects won't move in relation to each other (staying where the user put them).

ACTUAL OUTCOME: The objects snap back to the original relative position they were in when the joint was made.

--- DETAILS --- I have two objects attached by a configurable joint. When the objects are NOT interacting with the user, I want the joint to constrain their relative postition to each other. I accomplish this by setting the ConfigurableJoint's motionX/Y/Z and angularMotionX/Y/Z to "Locked".

However, I also want the user to be able to drag the objects around RELATIVE to each other.

I tried to accomplish this by setting the ConfigurableJoint's angular-motion to "Free" when one of the objects is being dragged around, and then setting it BACK to Locked when the user stops dragging on object.

But when I release the object, and re-lock the angular motion - the object snaps back to its original position.

 private ConfigurableJoint MakeConfigurableJoint(Collider other, int end)
 {
     Vector3 myAnchor;
     ConfigurableJoint tempJoint = this.gameObject.AddComponent("ConfigurableJoint") as ConfigurableJoint; // Neg
     tempJoint.autoConfigureConnectedAnchor = false; // Prevent joint from finding it's own position

         myAnchor = new Vector3(1f, 0, 0);
         connectedBody1 = other.gameObject;
         connectedBody1Script = connectedBody1.GetComponent<AtomScript>() as AtomScript;
     
     tempJoint.anchor = myAnchor; 
     tempJoint.connectedBody = other.transform.rigidbody;
     tempJoint.connectedAnchor = new Vector3(0, 0, 0);

     // PARAMETERS //
     // Motion
     tempJoint.xMotion = ConfigurableJointMotion.Locked;
     tempJoint.yMotion = ConfigurableJointMotion.Locked;
     tempJoint.zMotion = ConfigurableJointMotion.Locked;
     tempJoint.angularXMotion = ConfigurableJointMotion.Locked;
     tempJoint.angularYMotion = ConfigurableJointMotion.Locked;
     tempJoint.angularZMotion = ConfigurableJointMotion.Locked;

     tempJoint.axis          = new Vector3(0, 0, 0);
     tempJoint.secondaryAxis = new Vector3(0,0,0);

     return tempJoint;
 }//MakeConfigurableJoint

 // This DOES successfully allow me to move the object around the other
 public void loosenBonds()
 {
     tempJoint.angularXMotion = ConfigurableJointMotion.Free;
     tempJoint.angularYMotion = ConfigurableJointMotion.Free;
     tempJoint.angularZMotion = ConfigurableJointMotion.Free;
 }// loosenBonds

 // This DOES lock the movement again, but first it snaps the object back to the original location (doesn't leave it where I dragged it)
 public void tightenBonds()
 {
     tempJoint.angularXMotion = ConfigurableJointMotion.Locked;
     tempJoint.angularYMotion = ConfigurableJointMotion.Locked;
     tempJoint.angularZMotion = ConfigurableJointMotion.Locked;
 }// tightenBonds

 void OnMouseDown(){ loosenBonds() };
 void OnMouseUp()  { tightenBonds(); }
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 koan911 · May 22, 2015 at 12:59 AM 0
Share

This question is highly relevant to anyone desiring to emulate hydraulics using Unity/PhysX. The intuitive model for hydraulics is that a joint is driven by forces when active and is locked when inactive.

We have had to resort to using very high force levels combined with very high damping (or perhaps drag) levels combined with limiting ourselves to fairly slow target velocities. Otherwise our rig behaves like an assembly of soggy noodles. (We do taste this hack is going to catch up with us later.)

So: we also want exactly what the OP wants, which is to be able to dynamically lock a joint axis in its current position.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dwaldrum · Jan 22, 2017 at 09:44 PM

The proper way to do this is to actually change from using desired velocity to desired rotation in order to lock the joint.

So when you start driving the joint with a force, you unlock the joint:

 JointDrive drive = joint.angularXDrive;
 drive.positionSpring = 0.0f;
 drive.positionDamper = 50000.0f;
 drive.targetAngularVelocity = new Vector3(1.0f, 0.0f, 0.0f);
 joint.angularXDrive = drive;

Once you stop wanting to move the joint you then lock the joint:

 joint.targetRotation = GetCurrentRotation(joint);
 joint.targetAngularVelocity = new Vector3(0.0f, 0.0f, 0.0f);
 JointDrive drive = joint.angularXDrive;
 drive.positionSpring = 50000.0f;
 drive.positionDamper = 0.0f;
 joint.angularXDrive = drive;

The good thing about this is it gives your joint a realistic strength to its locking mechanism, depending on what you set your spring values to upon locking it can still react to forces and weights being applied.

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 Shiro_Yami · Feb 22, 2018 at 03:59 PM

Hi @dwaldrum, How can you get the current rotation of the joint please ?

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

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

Related Questions

In script, how to set ConfigurableJointMotion to locked without it reseting to default position 3 Answers

How do I set up a ConfigurableJoint so that it behaves just like a HingeJoint? 1 Answer

ConfigurableJoint to constrain objects to a straight track 0 Answers

consecutive angularxdrive.positionspring.equals() not working 0 Answers

angular limits 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