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 sidestepper · Jun 11, 2015 at 12:34 AM · physicshingejointhingemotorlimits

Hinge Motor Stuck After Hitting Limits

I have a door in my scene with the following structure:

-DoorObject
  -Frame
  -DoorOuter
    -DoorInner

DoorObject is just the container, it has a box collider setup as a trigger for interaction. Frame is just part of the mesh I imported as the door frame, containing just MeshFilter and Renderer. DoorOuter is where I've setup my HingeJoint and Rigidbody, DoorInner is the geometry and collider containing MeshFilter, MeshRenderer and MeshCollider(Convex=true, IsTrigger=false).

Hinge seems to be working fine, I can walk into it and push the door around on its hinge. I've added limits so the door opens one way and can spin to almost hitting the next interior wall(Like a real door). Hinge/Rigidbody properties are as such:

Rigidbody:

  • Drag: 0.5

  • Angular Drag: 0.5

  • Use Gravity: false

  • Kinematic: false

  • Interpolate: none

  • Collision Detection: Discrete

  • Constraints: all unchecked

HingeJoint:

  • Connected body: none

  • Axis stuff is fine, and anchor stuff is autoconfigured, this all seems fine

  • Use Spring: no

  • Use Motor: no(Changed at runtime)

  • Use Limits: yes
    • Min: 0

    • Max: 170

    • Min Bounce: 0

    • Max Bounce: 0

  • Break Force: infinity

  • Break Torque: infinity

  • Enable Collision: no

  • Enable preprocessing: yes

I have a script on the door for handling certain interactions. Side A is the side that can be pushed, if you were on side B, you would pull the door to open it. So from Side A, when the door is closed, you can just open it by pushing into it, once open, an interaction is available to close this door. Pressing the button to close the door from Side A does the following:

public void Close()
{
    JointMotor motor = _hinge.motor;
    motor.force = 50;
    motor.targetVelocity = 100f;
    _hinge.motor = motor;
}

Seems pretty simple, and this works, it will close the door and disables the motor once it closes using some logic in the door's update script:

private void Update()
{
    if (_hinge.useMotor) {
        if(_hinge.motor.force > 0f) {
            if(_door.localEulerAngles.y < 91f) {
                Debug.Log ("Was closing, turn off motor");
                _hinge.useMotor = false;
            }
        }
        else {
            if(_door.localEulerAngles.y > 150f) {
                Debug.Log ("Was opening, turn off motor");
                _hinge.useMotor = false;
            }
        }
    }
}

I know there's some magic numbers there, when the local y angle is approximately 90, the door is closed, and the max limit is set to around 180 on the hinge so it doesn't open through the wall, so when opening it kills the motor at 150, momentum will carry the door to its upper limit.

So, sorry for long explanation, wanted to be very clear, the problem:

When I push the door open from Side A, then hit the interaction to close the door, the motor turns on and it closes the door(I can even see the useMotor checkbox the inspector check on when closing and check off when done), then disables the motor once closed, then I can repeat this process. HOWEVER, when I push open the door hard enough and let it hit its upper limit, then the interaction does nothing, the door stays stuck on its upper limit. STRANGER YET, when stuck in this manner, the interaction still shows in the inspector that the useMotor is turned on, but does nothing - and weirdest of all, when stuck, if I check off the useMotor in the inspector, the door closes as if it was on a motor(This may just be a runtime issue with the hinge/motor and the inspector not reflecting values correctly, but I still found it very odd).

Hope this isn't TLDR, please let me know if any additional details are required, and thank you in advance for any assistance.

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
1

Answer by t36k · Mar 26, 2017 at 05:53 PM

I have a similar set up except in 2D. I have an object with a hinge joint and I flip it back and forth between its limits using the motor. And like you when my hinge joint hits the upper or lower angle limits in some cases it was getting stuck.

What I ended up doing was giving the limits some buffer. And instead of using the limit state provided by the hinge joint like this:

 private void Update()
 {
     if (joint.limitState == JointLimitState2D.LowerLimit ||
         joint.limitState == JointLimitState2D.UpperLimit)
     {
         startSpeed *= -1;
         SetMotorSpeed(startSpeed);
     }
 }

I opted to look at the joint angle and do my own comparison with a buffer of 5 degrees:

 private void Update()
 {
     if (joint.jointAngle <= joint.limits.min + 5f && 
         joint.motor.motorSpeed < 0f)
     {
         startSpeed *= -1;
         SetMotorSpeed(startSpeed);
     }
     else if (joint.jointAngle >= joint.limits.max - 5f &&
                 joint.motor.motorSpeed > 0f)
     {
         startSpeed *= -1;
         SetMotorSpeed(startSpeed);
     }
 }
 
 private void SetMotorSpeed(float speed)
 {
     motor = joint.motor;
     motor.motorSpeed = speed;
     joint.motor = motor;
 }

I've also noticed that sprites connected with a hinge joint will sometimes break apart or behave strangely when the hinge joint hits the angle limit.

My solution is kinda hacky but I need to finish my game. Seriously.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i change HingeJoint's axis from code? 0 Answers

hinge joint bounce min velocity - physical explanation please 0 Answers

Hinge Joint Motor Force Units? 0 Answers

Disable weight "inheritance" when using HingeJoint2D? 1 Answer

Why are my hinge joints inaccurate? 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