Search Unity

DragRigidbody by clicking anywhere

Discussion in 'Scripting' started by stuthemoo, Oct 22, 2011.

  1. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Hi,

    I'm using the DragRigidbody script to move my character around. I understand that it is as if there is a spring with one end attached to my cursor and the other end attached to the player.

    Code (csharp):
    1.  
    2. var spring = 50.0;
    3. var damper = 5.0;
    4. var drag = 10.0;
    5. var angularDrag = 5.0;
    6. var distance = 0.2;
    7. var DistanceOfObject = 1;
    8. var attachToCenterOfMass = false;
    9. var Enabled : boolean;
    10. private var springJoint : SpringJoint;
    11.  
    12. function Update ()
    13. {
    14.    
    15.     // Make sure the user pressed the mouse down
    16.     if (!Input.GetMouseButtonDown (0))
    17.        
    18.     return;
    19.    
    20.     var mainCamera = FindCamera();
    21.    
    22.     // We need to actually hit an object
    23.     var hit : RaycastHit;
    24.     if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, DistanceOfObject))
    25.     return;
    26.     // We need to hit a rigidbody that is not kinematic
    27.     if (!hit.rigidbody || hit.rigidbody.isKinematic)
    28.     return;
    29.    
    30.     if (!springJoint)
    31.     {
    32.         var go = new GameObject("Rigidbody dragger");
    33.         var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
    34.         springJoint = go.AddComponent ("SpringJoint");
    35.         body.isKinematic = true;
    36.     }
    37.    
    38.     springJoint.transform.position = hit.point;
    39.     if (attachToCenterOfMass)
    40.     {
    41.         var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
    42.         anchor = springJoint.transform.InverseTransformPoint(anchor);
    43.         springJoint.anchor = anchor;
    44.     }
    45.     else
    46.     {
    47.         springJoint.anchor = Vector3.zero;
    48.     }
    49.    
    50.     springJoint.spring = spring;
    51.     springJoint.damper = damper;
    52.     springJoint.maxDistance = distance;
    53.     springJoint.connectedBody = hit.rigidbody;
    54.    
    55.     StartCoroutine ("DragObject", hit.distance);
    56. }
    57.  
    58. function DragObject (distance : float)
    59. {
    60.     var oldDrag = springJoint.connectedBody.drag;
    61.     var oldAngularDrag = springJoint.connectedBody.angularDrag;
    62.     springJoint.connectedBody.drag = drag;
    63.     springJoint.connectedBody.angularDrag = angularDrag;
    64.     var mainCamera = FindCamera();
    65.     while (Input.GetMouseButton (0))
    66.     {
    67.         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
    68.         springJoint.transform.position = ray.GetPoint(distance);
    69.         yield;
    70.     }
    71.     if (springJoint.connectedBody)
    72.     {
    73.         springJoint.connectedBody.drag = oldDrag;
    74.         springJoint.connectedBody.angularDrag = oldAngularDrag *2;
    75.         springJoint.connectedBody = null;
    76.     }
    77. }
    78.  
    79. function FindCamera ()
    80. {
    81.     if (camera)
    82.     return camera;
    83.     else
    84.     return Camera.main;
    85. }
    86.  
    But I want there to be one difference, I want to be able to click anywhere on the screen and the player end of the string to always attach to a specific point on the player.

    I hope I make sense. Does anybody have an idea for how I can do this?

    Thank you,
    Stuart