- Home /
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;
}
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?
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. :/
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.
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!
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613070955im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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