The attack demo

Like our previous demos, we will start with a basic scene with a ground and walls. The demos here will involve an enemy ship attacking a player, so add a ship to the scene, name it player, and add simple controls to move the ship around. Also, tint the color of the material to make the player ship stand out from the enemy ship that we'll add in a moment. Of course, the player ship isn't an AI, so it doesn't need a RAIN AIRig, but it does need to have a RAIN Entity component. With the player selected, go to RAIN | Create Entity. Next, it needs a visual aspect for the AI enemies to see it; from the Add Aspect dropdown, select Visual Aspect and rename the aspect to player. This provides a base for our attack demo. This is how the RAIN attack demos will look with a player ship:

The attack demo

Next, we need an enemy for the attack. The enemies will also be ship models, and as we are focusing on just the AI, we won't worry about the actual game mechanics of attacking, such as having the ship fire projectiles at the player, then having the player respond to being hit, and so on. Usually, these kind of attack AI states involve playing different animations for the AI, and we will explore these more in Chapter 10, Animation and AI; for now, we just need a simple visualization to illustrate the attack. To visualize, we'll store a Boolean variable in RAIN's working memory flagging if the enemy is attacking and if so, start blinking.

To set this up, add a ship to the scene and add an AIRig to it by going to RAIN | Create AI. To add an attack flag, select the RAIN Memory tab on the ship AI (the light bulb icon) and from Add Variable, select bool. Rename the variable to isAttacking and leave it to the default value of false. The memory with the isAttacking variable set should look like the following screenshot:

The attack demo

To use this variable, create a new script called Enemy.cs and add it to the enemy ship. Change the code to the following:

The attack demo

Here, we store the AIRig for the entity by retrieving it at the start. Then, we get the isAttacking variable from the working memory, and if the enemy is attacking, the ship starts blinking green. If it's not attacking, the ship stays its default color, which is white. Create a new prefab in Unity named Enemy and drag the ship into it. Now we have enemies that can start attacking the player, and we can start setting up our AI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.135.58