- 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.
- Vue 3移動Web開發(fā)與性能調(diào)優(yōu)實(shí)戰(zhàn)
- Spring 5.0 By Example
- 數(shù)字媒體應(yīng)用教程
- C語言程序設(shè)計(jì)習(xí)題解析與上機(jī)指導(dǎo)(第4版)
- OpenCV 3和Qt5計(jì)算機(jī)視覺應(yīng)用開發(fā)
- jQuery從入門到精通 (軟件開發(fā)視頻大講堂)
- Python時間序列預(yù)測
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- 區(qū)塊鏈技術(shù)進(jìn)階與實(shí)戰(zhàn)(第2版)
- JavaScript應(yīng)用開發(fā)實(shí)踐指南
- Unity Character Animation with Mecanim
- Photoshop智能手機(jī)APP界面設(shè)計(jì)
- Python滲透測試編程技術(shù):方法與實(shí)踐(第2版)
- Getting Started with Windows Server Security
- C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)與習(xí)題精解