lerp not working

Hi,

I'm trying to do a simple lerp between positions and I cant get it working, I'm assuming I need to put the lerp command in a particular function? but I'm struggling to get it to work with my button presses.

var shelfpos : Vector3;
shelfpos = transform.position;
var otherObject : GameObject;
otherObject = GameObject.Find("First Person Controller/Main Camera/held_item");
private var startTime : float;
var duration : float = 1.0;
var inhand = false;
var origrot;

function Start () {
var shelfpos : Vector3;
origrot = transform.rotation;
}

function Update () {

var somePos : Vector3 = otherObject.transform.position; //get position of helditem object

// if object is selected move to helditem object

if (inhand == true)
{
            transform.position = Vector3.Lerp (shelfpos, somePos,Time.time);
}

if (inhand == false)
{
 transform.position = Vector3.Lerp (transform.position, shelfpos,Time.time);
 transform.rotation = origrot;
}

}

function OnMouseUp () {
inhand = true;
}

function OnGUI () {

if (inhand == true)
        if (GUI.Button (Rect (170,250,150,30),"Return To Shelf"))
            inhand = false;

}

Any help would be cool.

The key to your problem is that after the first second of your game, there can no longer be a lerp, because of your use of Time.time as the lerp value. To start off with, try resetting the lerp value to zero every time you change inhand. Also, name it inHand. And name origrot origRot while you're at it (if you really feel the need to shorten the name so much; originalRotation should be fine on today's computer monitors.). I thought it was some kind of demon name the first time I saw it.

Thanks for all the help.

I managed to get it working using the simplest solution I could find, which was using `Time.deltaTime` instead. I also added a `Quaternion.Slerp` to smooth out the rotation. I have enclosed the updated code in case somebody might find it useful. I have updated the variable names also, just out of interest why is it better to use inHand instead of inhand? anyway, heres the code...

var shelfpos : Vector3;
var otherObject : GameObject;
var inHand = false;
var originalRotation;
shelfpos = transform.position;
otherObject = GameObject.Find("First Person Controller/Main Camera/held_item");

function Start () {
var shelfpos : Vector3;
originalRotation = transform.rotation;
}

function Update () {
var speed = 6;
var somePos : Vector3 = otherObject.transform.position;     //get position of helditem object

// if object is selected move to helditem object

    if (inHand == true)
    {
            transform.position = Vector3.Lerp (transform.position, somePos,Time.deltaTime*speed);
    }

    if (inHand == false)
    {
            transform.position = Vector3.Lerp (transform.position, shelfpos,Time.deltaTime*speed);  // move back to shelfpos

        //rotate back to originalRotation
        transform.rotation =
        Quaternion.Slerp (transform.rotation, originalRotation, Time.deltaTime*speed);

    }
}

function OnMouseUp () {
inHand = true;
    }

function OnGUI () {

if (inHand == true)
        if (GUI.Button (Rect (170,250,150,30),"Return To Shelf"))
            inHand = false;         
    }

Kind Regards