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

DEFENSIVE state

So, let's start with the situation where the player comes from the right side and our enemy has already spotted him. We want our AI to take advantage of the wall that protects him, making it difficult for the player as well as demonstrating a human-like intention, rather than simply opening fire. The enemy will move towards the wall and stay there firing on the corner until the player arrives to that position.

The enemy will change from the PASSIVE state into the DEFENSIVE state, instead of OFFENSIVE, only because doing that gives him a slightly better advantage over the player. Being defensive on the first encounter shows some personality to the AI, which is very important when making the behavior of the computer character believable. In future chapters we'll learn how to use the environment to help define our AI character in depth:

Void Defensive () { 
if(playerPosition == "triggerR")
{
// Check if player is currently
located on the triggerR position
transform.LookAt(playerSoldier);
// Face the direction of the player
if(cover == false)
{
transform.position = Vector3.MoveTowards(transform.position,
wallPosition.position, walk);
}
if(cover == true)
{
coverFire();}
}
}

We added the core for the Defensive state that we want to implement on our enemy AI when the player is coming from the right side. We also added new variables, such as speed, cover, playerSoldier, and coverFire. First, we need to check if the player is currently positioned in the triggerR zone; if the result is positive, the character should move towards the cover position. Once the enemy AI is at the cover position, he can start firing against the player (coverFire). Now let's input the following situation–if the player is still alive, our enemy needs to move to another situation, otherwise it will be cornered, which isn't a good scenario for the character we are creating. Let's add that situation to our script.

We want our character to walk back and get inside the building, while always facing the player and firing at the same time. We could use another tactic, or decide to be more aggressive and confront the player directly, but for now let's stick to a simple strategy. We can add more complex behavior later:

if (playerPosition == "triggerM") 
{
transform.LookAt(playerSoldier); // Face the direction of the player
transform.position = Vector3.MoveTowards(transform.position,
buildingPosition.position, walkBack);
backwardsFire(); }

In this part of the code we added a situation where the player comes from the right side and is still alive going to the middle, so we needed to change the previous behavior to a new one. Our AI character goes from the cover position to a newer position that is inside the building, firing at the player the whole time. At this point, the enemy will keep getting back until one of the two characters die–either the player or the AI character. We close the situation where the player comes from the right side. Now that we have completed this part, we need to complete the scenario and add the last situation, which is where the player goes around the building and comes from the left side. Our AI will need to adapt to these circumstances and behave differently, so let's work on that part and complete the example.

主站蜘蛛池模板: 丽水市| 廊坊市| 乌拉特中旗| 连平县| 海伦市| 青州市| 黔江区| 峨眉山市| 丰镇市| 锦州市| 鱼台县| 通榆县| 绥滨县| 长春市| 宁波市| 泉州市| 湘乡市| 樟树市| 晋城| 凌海市| 中阳县| 旅游| 江川县| 兰考县| 宣化县| 会东县| 休宁县| 麦盖提县| 永善县| 朝阳县| 织金县| 黑山县| 南澳县| 读书| 永丰县| 乌鲁木齐市| 桦甸市| 中方县| 城步| 舟山市| 瑞安市|