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

Setting conditions

We will need to provide our enemy tank with a few conditions to transitions states. These are the actual parameters that will drive the functionality.

Let's begin with the Patrol state. In order for our enemy tank to go from Patrol to Shoot, we need to be in range of the player; in other words, we'll be checking the distance between the enemy and the player, which is best represented by a float value. So, in your Parameters panel, add a float and name it distanceFromPlayer. We can also use this parameter to determine whether or not to go into the Chase state.

The Shoot state and the Chase state will share a common condition, which is whether or not the player is visible. We'll determine this via a simple raycast, which will, in turn, tell us whether the player was in the line of sight or not. The best parameter for this is a Boolean, so create a Boolean and call it isPlayerVisible. Leave the parameter unchecked, which means false.

Now we'll assign the conditions via the transition connectors' inspector. To do this, simply select a connector. When selected, the inspector will display some information about the current transition and, most importantly, the conditions, which show up as a list. To add a condition, simply click on the + (plus) sign:

Let's tackle each transition one by one:

  • Patrol to Chase
    • distanceFromPlayer < 5
    • isPlayerVisible == true

The Patrol to Chase transition conditions

Chase to patrol gets a bit more interesting as we have two separate conditions that can trigger a transition. If we were to simply add two conditions to that transition, both would have to be evaluated to true in order for the transition to occur. But we want to check whether the player is out of range or out of sight. Luckily, we can have multiple transitions between the same two states. Simply add another transition connection as you normally would. Right-click on the Chase state and then make a transition to the Patrol state. You'll notice that you now have two transitions listed at the top of the inspector. In addition, your transition connection indicator shows multiple arrows instead of just one to indicate that there are multiple transitions between these two states. Selecting each transition in the inspector will allow you to give each one separate conditions:

  • Chase to Patrol (A)
    • distanceFromPlayer > 5
  • Chase to Patrol (B)
    • isPlayerVisible == false
  • Chase to Shoot
    • distanceFromPlayer < 3
    • isPlayerVisible == true
  • Shoot to Chase
    • distanceFromPlayer > 3
    • distanceFromPlayer < 5
    • isPlayerVisible == true
  • Shoot to Patrol (A)
    • distanceFromPlayer > 6
  • Shoot to Patrol (B)
    • isPlayerVisible == false

We now have our states and transitions set. Next, we need to create the script that will drive these values. All we need to do is set the values, and the state machine will handle the rest.

主站蜘蛛池模板: 阳谷县| 洛隆县| 安陆市| 股票| 武冈市| 彩票| 钟祥市| 巴彦县| 长顺县| 宿州市| 华亭县| 泰来县| 界首市| 赤水市| 察哈| 昌宁县| 包头市| 铜鼓县| 习水县| 宜良县| 合川市| 吉木乃县| 玛纳斯县| 嘉兴市| 通山县| 随州市| 赤壁市| 金溪县| 柘荣县| 光山县| 特克斯县| 顺义区| 耿马| 于田县| 元谋县| 昆明市| 青冈县| 井陉县| 揭东县| 万年县| 锡林郭勒盟|