How to Ground this Script ?

Hi, i’m very noob at scripts and i’m testing this one.
But it works in any case, floating or grounded.

Where can i put the collision detection or something like that ?

Sorry about my english if something is wrong :stuck_out_tongue:

Thank you very much for your time.

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;
       
}

Well, I solved the problem with the following script.

I hope it will help someone.

Its function is to make Drag and Drop with smooth movement while in contact with an object with TAG

var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;
var isgrounded : boolean = true;

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 && isgrounded == true)
       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 OnCollisionEnter(theCollision : Collision)
{
    if(theCollision.gameObject.tag == "ground")
    {
       isgrounded = true;
    }
}

function OnCollisionExit(theCollision : Collision)
{
    if(theCollision.gameObject.tag == "ground")
    {
       isgrounded = false;
    }
}

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")&& isgrounded == true)
    {
       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;
       
}