I have solved this problem by calling the function from outside, and using a break
to halt functions, then calling them from outside again, although this will serve as a placeholder for now.
This is the script I’m using, I just removed some variables so its quicker to read
function movement () {
var time : float = 5;
var elapsedTime : float = 0;
var start : Vector3 = transform.position;
while (time > elapsedTime)
{
transform.position = Vector3.Lerp (start, new.position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
if (elapsedTime >= time)
{movement2();
}
}
This function works, and the object then transitions into movement2 fine.
However when I add a yield WaitForSeconds(Number);
at the start of the script, as it transitions into movement2 it moves instantly for a small distance along the path, and then smoothly performs the rest of movement2.
Does anyone know the cause or how to fix this, as I want to be able to lerp smoothly but also delay the lerps when needed,
I tried Debug.Log and its saying that the time and elapsed time are approx the same with or without the yield Instruction.
Wow, unityscript yields are… I’m not sure what that is. Maybe save yourself the trouble and use a tween library with callbacks. However, if you’re going to cut out code and post it, post a working example of the problem, not the working version with the problem removed, because without all of the code people trying to help can only make wild guesses as to what the issue is (I don’t know why so many people do this, it seems awfully common though).
Best wild guess is that because start is no longer the actual start position for transform.position after your WaitForSeconds(); thus the interpolation might appear to jump back in time when starting.
It’s best to only cache positions for stuff you’re going to use on the same frame.
Ok I’m posting the full script.
Are you saying that I should keep all the movement (custom) functions in one (custom) function, so that the variable startingPos(Vector3) is set only once and doesn’t interfere with the Vector3.lerp?
var dest : Transform;
var dest2 : Transform;
var guide : boolean;
function Update () {
if (guide) {
movement();
}
}
function movement () {
yield WaitForSeconds (0.4);
var time : float = 3;
var elapsedTime : float = 0;
var startingPos : Vector3 = transform.position;
while (elapsedTime < time)
{
transform.position = Vector3.Lerp (startingPos, dest.position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
if (elapsedTime >= time)
{movement2();
}
}
function movement2 () {
var time : float = 3;
var elapsedTime : float = 0;
var startingPos : Vector3 = transform.position;
while (elapsedTime < time)
{
transform.position = Vector3.Lerp (startingPos, dest2.position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield;
}
if (elapsedTime >= time)
{movement3();
}
}