x


Stopping Vector3.Lerp

Hallo everybody,

I stumbled against a weird (in my eyes) problem while using Vector3.Lerp. If i place the lerp function inside an if statement which should control when the lerp should actually take place and when the object should stop, it does not behave like i want it to.

function Update () 
{   
    calculateDistance();

    if (canMove)
    {
       startMoving();
    }
}

function moveLetters(thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float)
{   
    var i = 0.0;
    var rate = 1.0/time;


    while(i <1.0)
    {  
       i += Time.deltaTime * rate;
       thisTransform.position = Vector3.Lerp(startPos, endPos, i);
       yield;
    }

}

function calculateDistance()
{   
    if(clonedLetter_1 != null)
    {
       distance_1 = Vector3.Distance(clonedLetter_1.transform.position,player.transform.position);
       distance = distance_1;

       if(distance < 5)
       {
            canMove = true;
       } else if (distance > 5)
        {
            canMove = false;
        }

       print(distance);
    }
}

function startMoving()
{

       if(clonedLetter_1 != null)
       { 
         moveLetters(clonedLetter_1.transform,clonedLetter_1.transform.position,target_1.transform.position,30.0);
         clonedLetter_1.transform.position.y = target_1.transform.position.y;
       }

       if(clonedLetter_2 != null)
       {
         moveLetters(clonedLetter_2.transform,clonedLetter_2.transform.position,target_2.transform.position,30.0);
         clonedLetter_2.transform.position.y = target_2.transform.position.y;
       }

       if(clonedLetter_3 != null)
       {
         moveLetters(clonedLetter_3.transform,clonedLetter_3.transform.position,target_3.transform.position,30.0);
         clonedLetter_3.transform.position.y = target_3.transform.position.y;
       }
}

The problematic if statement:

 if(distance < 5)
   {
        canMove = true;
   } else if (distance > 5)
    {
        canMove = false;
    }

I printed the results and canMove switches correctly between true and false. Distance also works correctly. The problem is, once the lerp is turned on, it wont stop, even though i have an if statement which should stop it each time the distance is bigger than 5. My question is if lerp is "update" independent (if such a thing exists) or if there is another to stop a Lerp movement.

I am fairly new to scripting so i bid my excuses if this is something obvious which i cant see to unravel.

Anyways, thanks in advance.

Hatzalex

more ▼

asked Dec 08 '11 at 08:24 AM

hatzalex gravatar image

hatzalex
61 7 9 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

hi hatzalex,

its easy and check your moving object reached end position means just break that loop....thats it... :-)

function moveLetters(thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float)
    {   
        var i = 0.0;
        var rate = 1.0/time;


        while(i <1.0)
        {  
           i += Time.deltaTime * rate;
           thisTransform.position = Vector3.Lerp(startPos, endPos, i);
           yield;
           if(startPos - endPos == Vector3.zero)
           {
             break;
           }
        }

    }
more ▼

answered Dec 08 '11 at 12:39 PM

sriram90 gravatar image

sriram90
460 33 37 47

Thanks sriram90, apperantly all i needed to do is to break the loop, thanks alot mate.

Modified code:

while(i <1.0)
{   
    i += Time.deltaTime * rate;
    thisTransform.position = Vector3.Lerp(startPos, endPos, i);
    yield;     

    if(!canMove)
    {
       break;
    }
}
Dec 08 '11 at 12:58 PM hatzalex
(comments are locked)
10|3000 characters needed characters left

Try printing i, see if it ever becomes >1.0.

The time calculations in your moveLetters function looks suspicious to me.. if rate is equal to 1.0 getting divided by time, then it will keep getting smaller and smaller.. and if i is Time.deltaTime multiplied by a number that's constantly getting smaller (and never >1.0) then I don't think your while statement can escape.

more ▼

answered Dec 08 '11 at 10:01 AM

Bicko gravatar image

Bicko
227 8 10 13

"i" indeed never surpasses 1, but isn't this how Lerp works? When it reaches 1, it means it reached his destination right? Anyway, i use that code snippet to ensure that the object does not slow down when it is close to the target object (code was written by Eric5h5 btw).

Dec 08 '11 at 11:00 AM hatzalex

distance > 5 || distance < 5...what of distance == 5?

Dec 08 '11 at 11:13 AM ks13

While the chance the distance is exactly 5 is pretty small, i already updated the code to take also take it in consideration with no effect. Thanks for pointing it out though :)

Dec 08 '11 at 11:44 AM hatzalex
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1365
x573
x262
x142

asked: Dec 08 '11 at 08:24 AM

Seen: 1077 times

Last Updated: Dec 08 '11 at 12:58 PM