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
    		}
    		}
    	}
    
    
    }
    }

The cause of your problem is missing yield statement inside while loop. Without it, the loop is infinite and causes Unity to stop responding.

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.