• 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
1
Question by Matt-Downey · Aug 12, 2011 at 10:22 PM · javascriptphysicsjointhingejointspring

Hinge joints not reaching target position

I have a $$anonymous$$nge that has the following settings:

Rigidbody:

Mass = 1 Ang Drag = 0 Constraints: frozen on all but Y-axis

Hinge Joint:

Spring = 1 Damper = 0.001 Target Position = 0

...

The problem being the $$anonymous$$nge joint fluctuates around the target position yet does not stop at 0, but instead +- ~6 degrees. Turning up the spring helps, but fails aesthetically.

Also, in physics I turned sleep angular velocity to zero, w$$anonymous$$ch did not help.

I also tried switc$$anonymous$$ng the damper to 0 and angular drag to 0.05 (the default), w$$anonymous$$ch worked no better.

I wanted to script the $$anonymous$$nge myself until I read changing a rigidbody's angular velocity can yield unrealistic results.

[edit:]

Initially, I planned not to share my solution script, but after having to rewrite it, I decided to share it. T$$anonymous$$s script is meant for a western style saloon door, w$$anonymous$$ch reacts to the player's movement. Increasing the spring in the HingeJoint and the script will make the door swing faster (and vice-versa) and increasing the damper will make it slow down faster. (I put damper at .1 and spring at 1 for a nice slow western feel). Furthermore, the mass of the player should be at least a magnitude of 3 (ie 1000x) greater in order for the door to perform optimally without getting in the way of gameplay.

 #pragma strict
 
 //next 3 are cached variables to increase performance
 var Trans : Transform;
 var Rigid : Rigidbody;
 var Hing : HingeJoint;
 
 //resting position in Eulers.y for the (saloon) door
 //ie where it is initially placed in the scene, called at start
 var rest : float;
 rest = Trans.eulerAngles.y;
 
 function Update ()
     {
     //t$$anonymous$$s line makes sure we aren't checking when the door is at rest (optimization)
     if(Hing.spring.spring < 2)
         {
         //when the door is barely moving
         if(Mathf.Approximately(0,Rigid.angularVelocity.y))
             {
             //make the door swing faster, also stops sleep
             //(we don't necessarily want the door to sleep)
             Hing.spring.spring += Time.deltaTime;
             //if the door is approximately at resting position
             if(Mathf.Approximately(rest,Trans.eulerAngles.y))
                 {
                 //make the door stop all angular velocity
                 Rigid.angularVelocity.y = 0;
                 //manually place the door at the resting position
                 Trans.eulerAngles.y = rest;
                 //manually force the door to sleep
                 Rigid.Sleep();
                 }
             }
         }
     }
 
 function OnCollisionEnter ()
     {
     //THIS IS IMPORTANT
     //if t$$anonymous$$s weren't here, the door spring would increment to infinity
     //making it swing super fast in the future, compounded each time the door is $$anonymous$$t
     //AND continue to reach approximately plus or minus 6 degrees, not perfect equilibrium position
     Hing.spring.spring = 1;
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Melanina · Jun 14, 2013 at 10:37 AM

I have the similar problem, because i use too small scale in my project ( unity have finite accuracy ) just try put your object to another empty object and rise $$anonymous$$m scale to see difference. I hope someone know how to change t$$anonymous$$s defect without scale manipulation.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to use Hinge Joints correctly inside a parent? 1 Answer

Pushing a cart with a vehicle (Not generating initial torque) 0 Answers

Disable weight "inheritance" when using HingeJoint2D? 1 Answer

pinball flipper using Hinge spring not strong enough 3 Answers

Strange physics behaviour with spring joints and side walls. 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