- Home /
 
Drag Rigid Bodies First Person Controller.js
I have been trying to create a "Drag Rigid Bodies First Person Controller" script. But i'm having some trouble. First is getting the ray cast to work correctly. I'm finding the code not working properly. I want there to be a raycast MAXdistance. Where I can control how far the player can reach for an object. Right now, if they can see it, they can pick it up. I want to send false for anything past MAXdistance.
The second thing i want is to have my object to go to center screen. no matter how what resolution you have for your computer.
var ray = mainCamera.ScreenPointToRay (Vector3(500,500,20));
I want something like
var ray = mainCamera.ScreenPointToRay (Vector3(50%,50%,20));
Full 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.GetMouseButtonDown (0))
         return;
 
     var cam : Transform = Camera.main.transform;
         
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(cam.position, cam.forward,  hit, 50)
         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.GetMouseButton (0))
     {
         var ray = mainCamera.ScreenPointToRay (Vector3(500,500,20));
         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;
 }
 
              Your answer
 
             Follow this Question
Related Questions
FPS rigidbody problem 0 Answers
Low framerate on physics initialization. 0 Answers
3rd person carrying objects,3rd person picking up objects 0 Answers
airplane collision detection problem 1 Answer