- Home /
Switch Statements & Corotines?
I'm picking up bits and pieces of unity. I've been doing pretty good so far, I was able to get an enemy AI setup last weekend. Then over the weekend I was looking in to using a switch statement instead of a long list of if/else statements, and now I've run in to problems with the ai.
I used some code I found on answers, basically I put the switch statement in to a function`
 function AI() {
    working = true;
    
         switch (aiState) {
     
         case AIState.FindCover :
          yield findCover();
                  Debug.Log("State FindCover fired off");
            break;
        
         
         case AIState.Shooting :
            yield stopAndPop();
          Debug.Log("State stopandPop FiredOff");
                 break;
                 
            case AIState.MoveTo :
           yield moveTo();
          Debug.Log("State moveTo fired Off");
                 break;
         
         case AIState.InCover :
              yield goToCover();
           Debug.Log("State go To Cover");
                break;
         
                    }
          working =false;           
 }
Here's the issue I have now, previously before I went down this route I had all of the move code in the update function, now I moved this to it's own function called moveTo().
When the state changes to moveTo I get this error: BCE0126: It is not possible to evaluate an expression of type 'void'. I can fix this by putting a yield; at the end of the moveto function.
While the yield; will fix the error, the animation plays choppy, I can see what is happening. Instead of moving the enemy from point A to point B completely before cycling through the switch statement again, the switch statement continues to cycle over and over again. How can I change this so the moveTo function completes before the switch statement recycles in the update loop?
Here is the moveTo function code:
 function moveTo(){
 
  Debug.Log("Moving to Cover...");
 if(Alive){
 var vdistance : float = Vector3.Distance(transform.position, TargetWaypoint.transform.position);
 
 if(vdistance > 0){
 transform.position = Vector3.Lerp(transform.position, TargetWaypoint.transform.position, Time.deltaTime * 2/vdistance);
 }else{
 aiState = AIState.InCover;
 }
 }
 }
`
Your answer
 
 
             Follow this Question
Related Questions
A finite state machine for game objects 0 Answers
Bestpractices for statemachines 0 Answers
Regular coded Finite State Machine vs Using FSM with Antares Universe? 1 Answer
Basic idle/walk Movement FSM as a beginner experiment 0 Answers
easiest way to implement finite state machine in javascript? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                