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

The Patrol state

While our tank is in the Patrol state, we check whether it has reached the destination point. If so, it finds the next destination point to follow. The FindNextPoint method basically chooses the next random destination point among the waypoints defined. If it's still on the way to the current destination point, it'll check the distance to the player's tank. If the player's tank is in range (which we choose to be 300 here), then it switches to the Chase state. The rest of the code just rotates the tank and moves forward:

    protected void UpdatePatrolState() 
    { 
      //Find another random patrol point if the current  
      //point is reached 
      if (Vector3.Distance(transform.position, destPos) <=  
      100.0f) 
      { 
        print("Reached to the destination point\n"+ 
        "calculating the next point"); 
 
        FindNextPoint(); 
      } 
 
      //Check the distance with player tank 
      //When the distance is near, transition to chase state 
      else if (Vector3.Distance(transform.position,  
      playerTransform.position) <= 300.0f) 
      { 
        print("Switch to Chase Position"); 
        curState = FSMState.Chase; 
      } 
 
      //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); 
    } 
    protected void FindNextPoint() 
    { 
      print("Finding next point"); 
      int rndIndex = Random.Range(0, pointList.Length); 
      float rndRadius = 10.0f; 
      Vector3 rndPosition = Vector3.zero; 
      destPos = pointList[rndIndex].transform.position +  
      rndPosition; 
 
      //Check Range to decide the random point  
      //as the same as before 
      if (IsInCurrentRange(destPos)) 
      { 
        rndPosition = new Vector3(Random.Range(-rndRadius,  
        rndRadius), 0.0f, Random.Range(-rndRadius,  
        rndRadius)); 
        destPos = pointList[rndIndex].transform.position +  
        rndPosition; 
      } 
    } 
    protected bool IsInCurrentRange(Vector3 pos) 
    { 
      float xPos = Mathf.Abs(pos.x - transform.position.x); 
      float zPos = Mathf.Abs(pos.z - transform.position.z); 
 
      if (xPos <= 50 && zPos <= 50) 
        return true; 
 
        return false; 
    }
主站蜘蛛池模板: 九龙城区| 庆安县| 沁源县| 南华县| 章丘市| 胶州市| 沧源| 乐昌市| 曲松县| 韩城市| 阳新县| 曲周县| 凤庆县| 依兰县| 苏尼特右旗| 龙井市| 图木舒克市| 房产| 海南省| 富川| 龙川县| 阿瓦提县| 什邡市| 辛集市| 都昌县| 宁乡县| 九江县| 徐闻县| 天水市| 崇阳县| 兴义市| 安新县| 彰武县| 来安县| 金寨县| 侯马市| 洪泽县| 克什克腾旗| 会昌县| 镇平县| 通山县|