- Practical Game AI Programming
- Micael DaGra?a
- 545字
- 2021-07-02 20:43:04
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.
- 大話PLC(輕松動漫版)
- 計算思維與算法入門
- ServiceNow Application Development
- 大學計算機基礎實驗教程
- Android Studio Essentials
- Learning Selenium Testing Tools(Third Edition)
- iOS編程基礎:Swift、Xcode和Cocoa入門指南
- 小學生C++創意編程(視頻教學版)
- 零基礎Java學習筆記
- Android Sensor Programming By Example
- WordPress Search Engine Optimization(Second Edition)
- Java EE基礎實用教程
- 歐姆龍PLC編程指令與梯形圖快速入門
- Practical Time Series Analysis
- Web前端開發精品課:HTML5 Canvas開發詳解