官术网_书友最值得收藏!

The Attack state

If the player tank is close enough to attack our AI tank, we will rotate the turret object to face the player tank, and then start shooting. Then, if the player tank goes out of range, the tank goes back to the Patrol state:

    protected void UpdateAttackState() 
    { 
      //Set the target position as the player position 
      destPos = playerTransform.position; 
 
      //Check the distance with the player tank 
      float dist = Vector3.Distance(transform.position,  
      playerTransform.position); 
 
      if (dist >= 200.0f && dist < 300.0f) 
      { 
        //Rotate to the target point 
        Quaternion targetRotation =  
        Quaternion.LookRotation(destPos -  
        transform.position); 
        transform.rotation = Quaternion.Slerp( 
        transform.rotation, targetRotation,  
        Time.deltaTime * curRotSpeed);   
 
        //Go Forward 
        transform.Translate(Vector3.forward *  
        Time.deltaTime * curSpeed); 
 
        curState = FSMState.Attack; 
      } 
      //Transition to patrol is the tank become too far 
      else if (dist >= 300.0f) 
      { 
        curState = FSMState.Patrol; 
      } 
 
      //Always turn the turret toward the player 
      Quaternion turretRotation =  
      Quaternion.LookRotation(destPos  
      - turret.position); 
 
      turret.rotation =  
      Quaternion.Slerp(turret.rotation, turretRotation,  
      Time.deltaTime * curRotSpeed);  
 
      //Shoot the bullets 
      ShootBullet(); 
    } 
    private void ShootBullet() 
    { 
      if (elapsedTime >= shootRate) 
      { 
        //Shoot the bullet 
        Instantiate(Bullet, bulletSpawnPoint.position,  
        bulletSpawnPoint.rotation); 
        elapsedTime = 0.0f; 
      } 
    } 

主站蜘蛛池模板: 温州市| 楚雄市| 凭祥市| 遂平县| 兴国县| 天柱县| 芜湖市| 彭阳县| 沙坪坝区| 辛集市| 泗阳县| 岫岩| 休宁县| 淳化县| 铜山县| 宜城市| 太湖县| 扎赉特旗| 左云县| 武夷山市| 清新县| 铜鼓县| 方山县| 西盟| 连平县| 松阳县| 大庆市| 通海县| 江孜县| 尤溪县| 巴青县| 安新县| 福海县| 兰西县| 噶尔县| 扎囊县| 泰和县| 玉屏| 崇州市| 德化县| 易门县|