[Solved] Change to local space with: Vector3.Lerp

Hi all,

I am trying to find a way to make the below script work locally as opposed to world coordinates.
For example, I’d like to constrain the boundaries of a sphere so it can only move a certain distance from it’s original center point.

Any ideas?

public var waiting : boolean = false;
var randomPosition : Vector3;
var moveDelay : float = 1;
function Update ()
{
   
    if (waiting == false)
    {
        SendMessage("LerpCube");
    }
        else
        {
        transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*1);
        }
}


function LerpCube()
{
    randomPosition = Vector3 (Random.Range( -1.62, 2.14 ), Random.Range( 2.42, -2.11 ), Random.Range( -0.00916, -0.00916));
    waiting = true;
    yield WaitForSeconds (moveDelay);
    waiting = false;
    //Debug.Log ("waited");
}



//randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));

Would it be okay to just do your lerp in local relation to it’s own local space?

public var waiting : boolean = false;
var randomPosition : Vector3;
var moveDelay : float = 1;
function Update ()
{

    if (waiting == false)
    {
        SendMessage("LerpCube");
    }
        else
        {
        transform.localPosition= Vector3.Lerp (transform.localPosition, (randomPosition + transform.localPosition), Time.deltaTime*1);
        }
}


function LerpCube()
{
    randomPosition = Vector3 (Random.Range( -1.62, 2.14 ), Random.Range( 2.42, -2.11 ), Random.Range( -0.00916, -0.00916));
    waiting = true;
    yield WaitForSeconds (moveDelay);
    waiting = false;
    //Debug.Log ("waited");
}



//randomPosition = Vector3 (Random.Range( 6,-6 ), Random.Range( 4, -4 ));
1 Like

Thanks, Nigey, that works perfecly!

1 Like