- Home /
CoRoutine help
Hello,
From this post (AI optimization) which attempts to optimize AI by skipping a step via coroutine I tried to set up something similar below.
The code however makes Unity crash. Am I missing something crucial?
#pragma strict
var MoveSpeed;
var MaxDist : float;
var MinDist : float;
public var flyer : int;
var normalizedSpeed : float;
var forwardForce : float;
private var velocity: Vector3;
private var distanceToPlayer : float;
var Player : Transform;
public var character : CharacterController;
private var AI_DELAY: float = 0.5f;
function Start ()
{
yield StartCoroutine(AIMovements());
}
function Update ()
{
//yield StartCoroutine(AIMovements());
//StartCoroutine(AIMovements);
//AIMovements();
}
function AIMovements ()
{
yield WaitForSeconds(Random.value * AI_DELAY);
while(true)
{
var movement = transform.forward;
if(flyer == 1){
if(distanceToPlayer >= MinDist) {
if(distanceToPlayer <= MaxDist) {
movement *= normalizedSpeed * forwardForce;
movement += velocity;
movement *= Time.deltaTime;
character.Move( movement );
}
}
transform.LookAt(Player);
}
if(flyer == 0){
if(distanceToPlayer >= MinDist) {
if(distanceToPlayer <= MaxDist) {
movement *= normalizedSpeed * forwardForce;
movement += velocity;
movement *= Time.deltaTime;
movement.y = Player.position.y; // stay on ground
character.Move( movement);
transform.LookAt(Vector3(Player.position.x, transform.position.y, Player.position.z)); // no rotate up to lookat
}
}
}
}
}
Answer by ArkaneX · Sep 07, 2013 at 07:16 AM
The cause of your problem is missing yield statement inside while loop. Without it, the loop is infinite and causes Unity to stop responding.
Ok cool thanks, ArkaneX. So I should do something as below?
Strike previous comment out about enemies going grey. I had a misplaced gameObject.SetActiveRecursively(false); ...
However, I am now seeing the frame rate drop over time to nearly 0 from 60 ...
function AI$$anonymous$$ovements () {
while(true)
{
yield WaitForSeconds(Random.value * AI_$$anonymous$$AY);
var movement = transform.forward;
if(flyer == 1){
if(distanceToPlayer >= $$anonymous$$inDist) {
if(distanceToPlayer <= $$anonymous$$axDist) {
movement *= normalizedSpeed * forwardForce;
movement += velocity;
movement *= Time.deltaTime;
character.$$anonymous$$ove( movement );
}
}
transform.LookAt(Player);
}
if(flyer == 0){
if(distanceToPlayer >= $$anonymous$$inDist) {
if(distanceToPlayer <= $$anonymous$$axDist) {
movement *= normalizedSpeed * forwardForce;
movement += velocity;
movement *= Time.deltaTime;
movement.y = Player.position.y; // stay on ground
character.$$anonymous$$ove( movement);
transform.LookAt(Vector3(Player.position.x, transform.position.y, Player.position.z)); // no rotate up to lookat
}
}
}
}
}
Heh - I didn't know that comments under answer which is converted to comment disappear.
Anyway - repeating what I wrote previously, I don't see a framerate drop in my sample project. Unfortunately I don't know what is your project setup, so I can't mimic it. I set some random variables and objects in inspector though, and everything worked at over 60fps for over a $$anonymous$$ute.
One more thought - have you probably uncommented some code in your Update method? If yes, then you're running more and more coroutines and this might affect performance.
Answer by RyanZimmerman87 · Sep 07, 2013 at 09:12 AM
Do you ever set it to false? Or is it supposed to just infinitely move them.
I really think you should have some kind of control to ensure that their movements complete before you call them again.
I never used random. Value I always use Random.Range. But I'm guessing what's happening here is that Random.Range can roll very small numbers it's any float value between 0.0 and 1.0 correct?
Not sure if it can only be 0 OR 1 for the value like your conditions suggest but it seems strange to use a float for that if so.
So if you got .1 x .5 (your AI_Delay) that is .05 seconds. So you are potentially calling these moves to barely yield whatsoever and your frame rate is dropping because your processor cannot do all your AI movements before an additional yield timer is met and function called.
So you might start with just 1 yield function but I believe this number grows higher and higher and the higher. As it gets higher each movement will individually take longer cause so much other stuff is going on so it's a vicious cycle.
In short you need to ditch that while loop OR add a condition that only one movement can occur at a time.
Basically instead of having all this in inside a while loop you should just use StartCoroutine and Ienumerator. once the IEnumerator completes you call another StartCoroutine.
That would ensure that only 1 process can be going at a time where currently who knows what is going on that set up doesn't quite make sense.
For example:
float randomNumber = Random.value * AI_DELAY;
Start()
{
StartCoroutine (AI_Movements(randomNumber));
}
void restartFunction()
{
randomNumber = Random.value * AI_DELAY;
StartCoroutine (AI_Movements(randomNumber));
}
IEnumerator AI_Movements(float randomNumber)
{
yield return new WaitForSeconds(randomNumber);
//all your movement logic
restartFunction()
}
Apologies for C# that's all I use. But that example is how I would handle this kind of thing.
Your answer
Follow this Question
Related Questions
Coroutines randomly stop working 0 Answers
Best way to make melee AI damage player? 2 Answers
Enemy can only move in one direction at a time using transform.Translate and Coroutines? 1 Answer
FixedUpdate has stopped working 0 Answers
How can you tell if a gameobject is looking directly at another gameobject? 1 Answer