- Home /
continue moving without yielding.
I've a character that walks from waypoint 0 to 1 from 1 to 2 from 2 to 3. the character reaches from 0 to 1 it waits 1.5 seconds, and then when it gets to the 2nd waypoint it doesn't wait 1.5 seconds, and so on..
http://09duck.pastebin.com/qVREn4NB
var waypoint : Transform[]; var speed : float = 3; var currentWaypoint : int; var loop = true; var test;
function Update () { animation.wrapMode = WrapMode.Loop; var controller : CharacterController = GetComponent(CharacterController); if(currentWaypoint < waypoint.length){ var target : Vector3 = waypoint[currentWaypoint].position; var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude <= 1.5){
print("magniute");
animation.CrossFade("idle");
speed = 0;
WaitThreeSeconds();
if(test){
test = false;
currentWaypoint++;
}
if(currentWaypoint == 3){ currentWaypoint -= 3; }
}
else{
animation.CrossFade("walk");
speed = Random.Range(0.8,2.01);
RotateTowards();
}
} //print(moveDirection.magnitude); controller.SimpleMove(moveDirection.normalized * speed); }
function RotateTowards () { var target : Vector3 = waypoint[currentWaypoint].position; var moveDirection : Vector3 = target - transform.position; while(Vector3.Distance(target, transform.position) >= 1.5){ transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(moveDirection), 0.5* Time.deltaTime); transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); yield; } }
function WaitThreeSeconds(){ yield WaitForSeconds(1.5); test = true; }
You don't really say what your problem is, perhaps you could be more clear
I think AnaRhisT is trying to say that the delay only works the for the first way point
You're yielding inside of Update(), and you shouldn't do that. You can ONLY yield with a coroutine, nothing else (at least, that's the only recommended way, anyway).
so if i want to use yield even if i refer it a function i cant use the update in any way?
No, that's different. The reason it won't work in Update itself is that Yield holds up the routine running that particular function. So the rest of your Update would not be called until the yield releases it, which would likely cause odd behaviour.
Answer by Mike 3 · Jun 22, 2010 at 08:16 PM
Try put WaitThreeSeconds(); down by a line into the if(test) block
That should sort it
Thanks for that hint, i also needed to check "if(!test)"
You're doing the exact same thing again. You really shouldn't be calling all these coroutines from inside update. You probably have about 300 coroutines running simultaneously moving your object. if you change your while to if in RotateTowards, and remove the yield, it should fix things properly
Answer by AnaRhisT · Jun 22, 2010 at 10:35 PM
Solution :
var waypoint : Transform[]; var speed : float = 3; var currentWaypoint : int; var loop = true; var test;
function Update () { animation.wrapMode = WrapMode.Loop; var controller : CharacterController = GetComponent(CharacterController); if(currentWaypoint < waypoint.length){ var target : Vector3 = waypoint[currentWaypoint].position; var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude <= 1.5){
test = true;
print("magniute");
animation.CrossFade("idle");
speed = 0;
if(test){
WaitThreeSeconds();
currentWaypoint++;
}
if(currentWaypoint == 3){ currentWaypoint -= 3; }
}
else{
if(!test){
animation.CrossFade("walk");
speed = Random.Range(0.8,2.01);
RotateTowards();
}
}
} //print(moveDirection.magnitude); controller.SimpleMove(moveDirection.normalized * speed); }
function RotateTowards () { var target : Vector3 = waypoint[currentWaypoint].position; var moveDirection : Vector3 = target - transform.position; while(Vector3.Distance(target, transform.position) >= 1.5){ transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(moveDirection), 0.5* Time.deltaTime); transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); yield; } }
function WaitThreeSeconds(){ yield WaitForSeconds(1.5); test = false; }