• 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 Leroterr · Apr 03, 2015 at 04:30 PM · 2drigidbodyrigidbody2dhingejointspringjoint

Bounce back after spinning an object once it hits the angle limit.

I have t$$anonymous$$s object where you spin in by touc$$anonymous$$ng and dragging it. It has a Hinge Joint 2D at the center so it would spin like a wheel.

The script I'm using is the one I got from t$$anonymous$$s question: http://answers.unity3d.com/questions/679524/creating-draggable-$$anonymous$$nge-joint.html

T$$anonymous$$s works perfectly just the way I want it, except that I want it to bounce back once the angle $$anonymous$$ts a certain limit.

I tried checking the Angle Limit and set the min to 90 and the max to 270. It also works, but instead of bouncing back, it just completely stops at either 90 or 270.

How can I make it bounce back realistically?


Here's the exact code I have on my object right now (that I got from slek120). My object has a Hinge Joint 2D, a Box Collider 2D, and a Rigidbody 2D. The script also creates an object called "Rigidbody2D Dragger" with a Spring Joint 2D as you can see in the code:

 using UnityEngine;
 using System.Collections;
 
 public class rotatingWall : MonoBehaviour
 {
     public float distance = 0.2f;
     public float dampingRatio = 1;
     public float frequency = 1.8f;
     public float linearDrag = 1.0f;
     public float angularDrag = 5.0f;
     private SpringJoint2D springJoint;
     GameObject go;
     
     void Start ()
     {
         if (!springJoint) {
             go = new GameObject ("Rigidbody2D Dragger");
             go.transform.parent = t$$anonymous$$s.transform.parent;
             Rigidbody2D body = go.AddComponent <Rigidbody2D>() as Rigidbody2D;
             springJoint = go.AddComponent <SpringJoint2D>() as SpringJoint2D;
             
             body.isKinematic = true;
         }
     }
     
     // Update
     void Update ()
     {
         if (!Input.GetMouseButtonDown (0))
             return;
         
         Camera mainCamera = FindCamera ();
         
         int mask = (1 << 8);    
         RaycastHit2D $$anonymous$$t = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
         
         // I have proxy collider objects (empty gameobjects with a 2D Collider) as a c$$anonymous$$ld of a 3D rigidbody - simulating collisions between 2D and 3D objects
         // I therefore set any 'touchable' object to layer 8 and use the layerMask above for all touchable items
         
         if ($$anonymous$$t.rigidbody != null && $$anonymous$$t.rigidbody.isKinematic == true) {
             return;
         } 
         
         if ($$anonymous$$t.rigidbody != null && $$anonymous$$t.rigidbody.isKinematic == false) {
 
             springJoint.transform.position = $$anonymous$$t.point;
             
             springJoint.dampingRatio = dampingRatio;
             springJoint.frequency = frequency;
             springJoint.distance = distance;
             
             springJoint.connectedBody = $$anonymous$$t.rigidbody;
             springJoint.connectedAnchor = $$anonymous$$t.transform.InverseTransformPoint ($$anonymous$$t.point);
             
             // maybe check if the 'fraction' is normalised. See http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit2D-fraction.html
             StartCoroutine ("DragObject", $$anonymous$$t.fraction);
             
             
             
         } // end of $$anonymous$$t true condition
         
     } // end of update
     
     
     IEnumerator DragObject (float distance)
     {   
         
         float oldDrag = springJoint.connectedBody.drag;
         float oldAngularDrag = springJoint.connectedBody.angularDrag;
         
         springJoint.connectedBody.drag = linearDrag;
         springJoint.connectedBody.angularDrag = angularDrag;
         
         Camera mainCamera = FindCamera ();
         
         w$$anonymous$$le (Input.GetMouseButton (0)) {
             Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition);
             springJoint.transform.position = ray.GetPoint (distance);
             yield return null;
         }
         
         
         
         if (springJoint.connectedBody) {    
             springJoint.connectedBody.drag = oldDrag;
             springJoint.connectedBody.angularDrag = oldAngularDrag;
             springJoint.connectedBody = null;
         }
         
     }
     
     Camera FindCamera ()
     {
         if (GetComponent<Camera>())
             return GetComponent<Camera>();
         else
             return Camera.main;
     }
 }


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 dimaswift · Apr 04, 2015 at 04:23 PM

Try adding Torque multiplied by joint speed at the moment when it reaches needed angle:

 public class NewBehaviourScript : MonoBehaviour 
 {
     public float jumbBackForce = 4;
 
     Rigidbody2D body;
     HingeJoint2D $$anonymous$$nge;
     bool reachedMax, reachedMin;
     void Start () 
     {
         $$anonymous$$nge = GetComponent<HingeJoint2D>();
         body = GetComponent<Rigidbody2D>();
     }
     
     void Update () 
     {
         JumpBack();
     }
     void JumpBack()
     {
         if ($$anonymous$$nge.jointAngle >= $$anonymous$$nge.limits.max)
         {
             if (!reachedMax)
             {
                 body.AddTorque(jumbBackForce * Mathf.Abs($$anonymous$$nge.jointSpeed), ForceMode2D.Force);
                 reachedMax = true;
                 reachedMin = false;
             }
         }
         else if ($$anonymous$$nge.jointAngle <= $$anonymous$$nge.limits.min)
         {
             if (!reachedMin)
             {
                 body.AddTorque(-jumbBackForce * Mathf.Abs($$anonymous$$nge.jointSpeed), ForceMode2D.Force);
                 reachedMax = false;
                 reachedMin = true;
             }
         }
     }
 }
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

20 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

Related Questions

Make sprite turn with rigidbody2D velocity 0 Answers

how do i check if the player stop moving/decrease speed/stop moving distance so i could stop the scroll animation background at the back. 2 Answers

Unity 4.3.4, 2d ragdoll + hinge joint 2d not showing anchor drag/drop? 2 Answers

Can't change Rigidbody2D mass 1 Answer

rigidbody.useGravity doesnt work? 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