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

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

主站蜘蛛池模板: 临夏县| 安化县| 大荔县| 临沭县| 邯郸市| 信宜市| 穆棱市| 靖西县| 安图县| 洛川县| 叶城县| 迁西县| 泗阳县| 即墨市| 岑溪市| 惠安县| 镇平县| 海宁市| 无锡市| 夏河县| 桂林市| 嘉义县| 谷城县| 石林| 资源县| 安国市| 佛冈县| 古交市| 东方市| 双流县| 东宁县| 通山县| 平乡县| 图木舒克市| 上思县| 孝感市| 宁国市| 修水县| 济阳县| 彭泽县| 井陉县|