- Home /
The question is answered, right answer was accepted
modifying dragrigidbody script
Hello, i have been trying to make the drag rigidbody script work without a camera this meant i have to change some lines of code but i have gotten stuck at a particular point, i want to know how to achieve the same effect by using a different method.
i am trying to drag the rigid body(ragdoll) with an object(dragger) and i am stuck at this point in the code
var dir = Vector3.zero;// direction of the object to be dragged
var target = Vector3.zero; // position of the object
var myTransform: Transform;
function Awake()
{
myTransform = transform; // for better performance
}
function Update ()
{
var dir = target - myTransform.position;
Debug.DrawRay(myTransform.position, dir, Color.green);
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
// check if the ragdoll can be carried
if(target == null)
return;
// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(myTransform.position,dir, hit, 100))
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;
while (Input.GetMouseButton (0))
{
// the current code i am using
var ray = new Ray (myTransform.positon, myTransform.forward);
//var ray = main
//var ray: Ray;
springJoint.transform.position = ray.GetPoint(distance);
yield;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
it gives me null exception reference anytime i run it NullReferenceException: Object reference not set to an instance of an object dragPrey+$DragObject$38+$.MoveNext () (at Assets/myScripts/dragPrey.js:83) UnityEngine.MonoBehaviour:StartCoroutine(String, Object) dragPrey:Update() (at Assets/myScripts/dragPrey.js:72)
please i would like to know what is wrong with code above thanks.
Thanks. If no one else gives you an answer, I'll look over it when I have a bit more time. I've helped several people make changes to this script in the last couple of weeks.
thanks for your readiness to help me robertbu, i eventually found a way but we are not allowed to post answers to our questions so i am closing this, thanks a lot
Occasionally there are browser issues, but generally anyone can post an answer to their own question. I'm glad you got it fixed.
Follow this Question
Related Questions
2D Rigidbody Drag with Mouse 0 Answers
Realistic Bow and Arrow Physics? 2 Answers
change to raycast shooting instead of rigidbody shooting 1 Answer
Why is CapsuleCast/Raycast performance effected by rigidbody? 1 Answer
Raycast On Rigidbody 1 Answer