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 SuperMasterBlasterLaser · Apr 03, 2014 at 12:05 PM · rotationphysicsrigidbodyhingejoint

Creating draggable hinge joint

Hello everyone.

I am using 2D mode of editor. I have createt two sprites: one long rectangle, second is short rectangle. I have attached to both of them BoxCollider2D's and rigidbodies.

I have made square rigidbody kinematic. After that, I have added to rectangle Hinge joint and connected it with rigidbody of square.

So i have rectangle that is fixed at square and spins around it when other objects will hit it.

Question: How can I make this carousel controllable with touch and mouse, like if I start to drag it, it will rotate to position of my touch/mouse? Also when I release it, it will rotate with speed of my last swipe and start to slow down until it stops.

PS. Gravity is zero.

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 SuperMasterBlasterLaser · Apr 05, 2014 at 01:32 PM 0
Share

Helloooooo, anybody hear me!?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by slek120 · Apr 05, 2014 at 01:47 PM

You can use the DragRigidBody2D script Here is my version of the script

 using UnityEngine;
 using System.Collections;
 
 public class DragRigidBody2D : 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;
 
     void Start ()
     {
         if (!springJoint) {
             GameObject go = new GameObject ("Rigidbody2D Dragger");
             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 hit = Physics2D.Raycast (mainCamera.ScreenToWorldPoint (Input.mousePosition), Vector2.zero, Mathf.Infinity, mask);
 
         // I have proxy collider objects (empty gameobjects with a 2D Collider) as a child 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 (hit.rigidbody != null && hit.rigidbody.isKinematic == true) {
             return;
         } 
 
         if (hit.rigidbody != null && hit.rigidbody.isKinematic == false) {
 
             springJoint.transform.position = hit.point;
             
             springJoint.dampingRatio = dampingRatio;
             springJoint.frequency = frequency;
             springJoint.distance = distance;
 
             springJoint.connectedBody = hit.rigidbody;
             
             
             // maybe check if the 'fraction' is normalised. See http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit2D-fraction.html
             StartCoroutine ("DragObject", hit.fraction);
             
             
             
         } // end of hit 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 ();
         
         while (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 (camera)
             return camera;
         else
             return Camera.main;
     }
 }


Comment
Add comment · Show 5 · 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 SuperMasterBlasterLaser · Apr 06, 2014 at 10:22 AM 0
Share

Not working. I want for HingeJoint2D not SpringJoint2D. Even when I changed to SpringJoint2D it does not work.

avatar image slek120 · Apr 06, 2014 at 11:17 AM 0
Share

You don't have to change anything you have. DragRigidBody2D makes a kinematic rigidbody2D with a springJoint2D component. If you click a rigidbody, it will connect the springJoint to that rigidbody. Then you can pull it by moving the mouse. This also translates to touch without changing any code.

One problem with the previous script is that it connects to the center of the connected rigidbody. $$anonymous$$aybe this is why it did not work. Try adding this around line 50.

 springJoint.connectedAnchor = hit.transform.InverseTransformPoint (hit.point);
avatar image SuperMasterBlasterLaser · Apr 07, 2014 at 01:56 AM 0
Share

I have added this line, it is still does not work. I click this object, try to drag it, it just frozen to its place.

avatar image slek120 · Apr 07, 2014 at 04:34 PM 0
Share

Oh, I'm sorry. There is a layer mask in the RaycastHit2D. This way, you can configure only certain objects to be draggable. To fix this, you can change line 32 to the number of the layer that the object is on (default = 0) or you can change line 33 to

 RaycastHit2D hit = Physics2D.Raycast (mainCamera.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);

Does this fix your problem? Tell me if it doesn't.

avatar image SuperMasterBlasterLaser · Apr 13, 2014 at 09:49 AM 0
Share

That worked!. Fantastic, thank you.

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

21 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

Related Questions

How to limit the motion direction of a GameObject? 1 Answer

Rotating a Rigidbody with Physics 1 Answer

How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer

Camera and mesh not colliding 1 Answer

drag object including free rotation 0 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