Unity Artificial Intelligence Programming
上QQ阅读APP看书,第一时间看更新

The Chase state

Similarly, while the tank is in the Chase state, it checks its distance with the player tank. If it's close enough, it'll switch to the Attack state. If the player tank has gone too far, then it'll go back to the Patrol state:

    protected void UpdateChaseState() 
    { 
      //Set the target position as the player position 
      destPos = playerTransform.position; 
 
      //Check the distance with player tank When 
      //the distance is near, transition to attack state 
      float dist = Vector3.Distance(transform.position,  
      playerTransform.position); 
 
      if (dist <= 200.0f) 
      { 
        curState = FSMState.Attack; 
      } 
      //Go back to patrol is it become too far 
      else if (dist >= 300.0f) 
      { 
        curState = FSMState.Patrol; 
      } 
 
      //Go Forward 
      transform.Translate(Vector3.forward * Time.deltaTime *  
      curSpeed); 
    }