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

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.

主站蜘蛛池模板: 清苑县| 邹平县| 逊克县| 色达县| 汝阳县| 紫阳县| 松桃| 平顺县| 抚顺县| 宜州市| 固原市| 靖边县| 海晏县| 广东省| 嵊州市| 宁乡县| 东乡族自治县| 兰西县| 台前县| 益阳市| 东丽区| 上饶市| 桂林市| 昌黎县| 济南市| 大英县| 张家港市| 鱼台县| 隆林| 神木县| 靖西县| 乐安县| 平安县| 建阳市| 营口市| 奉节县| 铜山县| 合川市| 临西县| 江山市| 梅州市|