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 LukaKotar · May 01, 2012 at 07:57 PM · dragrigidbody

DragRigidbody Not Working Properly

Hi.

I've got some issues with the DragRigidbody.js script that was imported with Standard Assets. The problem is, I can only grab an object if I am really really close, and it does not always rotate with the camera. Is there any way I could fix it? Or can I use any other way to grab the objects?

Here is the script:

     var spring = 50.0;
 var damper = 5.0;
 var drag = 10.0;
 var angularDrag = 5.0;
 var distance = 0.2;
 var attachToCenterOfMass = false;
 
 private var springJoint : SpringJoint;
 
 function Update ()
 {
     // Make sure the user pressed the mouse down
     if (!Input.GetButtonDown ("Fire1"))
         return;
 
     var mainCamera = FindCamera();
         
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, Mathf.Infinity))
         return;
     // We need to hit a rigidbody that is not kinematic
     if (!hit.rigidbody || hit.rigidbody.isKinematic)
         return;
     
     if (!springJoint)
     {
         var go = new GameObject("Rigidbody dragger");
         var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
         springJoint = go.AddComponent ("SpringJoint");
         body.isKinematic = true;
     }
     
     springJoint.transform.position = hit.point;
     if (attachToCenterOfMass)
     {
         var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
         anchor = springJoint.transform.InverseTransformPoint(anchor);
         springJoint.anchor = anchor;
     }
     else
     {
         springJoint.anchor = Vector3.zero;
     }
     
     springJoint.spring = spring;
     springJoint.damper = damper;
     springJoint.maxDistance = distance;
     springJoint.connectedBody = hit.rigidbody;
     
     StartCoroutine ("DragObject", hit.distance);
 }
 
 function DragObject (distance : float)
 {
     var oldDrag = springJoint.connectedBody.drag;
     var oldAngularDrag = springJoint.connectedBody.angularDrag;
     springJoint.connectedBody.drag = drag;
     springJoint.connectedBody.angularDrag = angularDrag;
     var mainCamera = FindCamera();
     while (Input.GetButton ("Fire1"))
     {
         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
         springJoint.transform.position = ray.GetPoint(distance);
         yield;
     }
     if (springJoint.connectedBody)
     {
         springJoint.connectedBody.drag = oldDrag;
         springJoint.connectedBody.angularDrag = oldAngularDrag;
         springJoint.connectedBody = null;
     }
 }
 
 function FindCamera ()
 {
     if (camera)
         return camera;
     else
         return Camera.main;
 }
Comment
Add comment · Show 3
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 LukaKotar · May 01, 2012 at 09:30 PM 0
Share

I've tried setting the "distance" variable from 0.2 to 2.0, but it didn't work either. Anyone got it to work? If so, how?

avatar image LukaKotar · May 02, 2012 at 04:58 AM 0
Share

O$$anonymous$$, now the player grabs the object and all that, but he can't grab when he isn't close enough. The distance is the only thing left that bugs me with this script. :/

avatar image Seth-Bergman · May 02, 2012 at 05:26 AM 0
Share

looks like distance is the right variable..ins$$anonymous$$d of 2, try setting it to 200. Probably obvious, but you'll need to change it in the inspector, not the script itself.

2 Replies

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

Answer by LukaKotar · May 05, 2012 at 10:45 AM

I had to attach the Spring Joint manually, and set the Max Distance and Min Distance. It works now!

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 Seth-Bergman · May 02, 2012 at 05:32 AM

Looks like distance is the right variable..instead of 2, try setting it to 200. Probably obvious, but you'll need to change it in the inspector, not the script itself. According to the documentation here:

http://unity3d.com/support/documentation/Components/class-SpringJoint.html

SpringJoint.maxDistance is what decides the acceptable distance, which is what the var "distance" is linked to, so this should definitely work

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
avatar image LukaKotar · May 03, 2012 at 02:13 PM 0
Share

Thanks, but it didn't work

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

DragRigidbody problem: Player moves with the object. 1 Answer

Dragging and rotating a Rigidbody2D (Limbs with HingeJoint). Shadow Play / Puppetry Simulation 1 Answer

When dragging a Door open using RIGIDBODYSCRIPT Plays sound. 1 Answer

Drag Rigidbody 2 Answers

How to calculate projectile (baseball) distance with drag factor 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