[#8] Girl

First of all create a new empty Game Object in the Scene Hierarchy and name it “Enemy”.

Then, as shown in the image above, locate the SapphiArtchan prefab from your myUnityAssets folder and drag it onto the Enemy object to make it a child.

Let’s rename SapphiArtchan “girl” for simplicity.

In the inspector window for the Enemy object add a Capsule Collider component and edit it so that if fits nicely around the girl’s body, as shown in the image above. Make sure to click the Edit Collider button to allow you to resize it by dragging. Also make sure to give the girl child object a rigid body so that it will interact with the physics of the world. Also check the Use Gravity and Is Kinematic boxes. Is Kinematic will allow the girl to collide with objects, such as the bullet, but not be affected by any forces (aside from gravity) within the game world.

Next, click on the small gear icon next to the Sapphi Art Chan_Anim C# script, and click Remove Component. C# scripts are not compatible with STYLY, so we need to remove this.

Also make sure to set the girl’s Y value to -1 so that she appears at the correct height above the stage.

Now create a new FSM in the Enemy object with a starting Setup state, then get and store the main camera as a Game Object called mainCamera and also find the child named “girl” and store it in a variable with the same name. We will need to reference both the main camera and the girl later on in the FSM so storing them here as variables is highly convenient.

Next, create a new state called Chasing Player and connect it to the FINISHED transition from the Setup state. Then create a new event called reachedPlayer and add it to Chasing Player.

Now add a Smooth Look At action and a Move Towards action to the Chasing Player state, as shown in the picture below.

The Smooth Look At action turns the girl to face the main camera and by extension the player in VR, since they take the perspective of the main camera. The Move Towards action then moves the girl towards the player, which is a key part of the game. Set the Finish Distance to be 5 so that the girls end up a reasonable distance away from the player and set the reachedPlayer event to trigger once this move action is complete.

When reachedPlayer is triggered, we want to transition to a separate called Reached Player. Create this state and connect it to the reachedPlayer transition. In the ReachedPlyaer state, add a Get Distance action that calculates the distance between the enemy (Use Owner) and the mainCamera variable (the player’s current position). Do this every frame. Then add a Float Compare action that results in the chasePlayer transition if the distance is greater than 1.5 (if Float 1 is greater than Float 2). This will cause the enemies to keep chasing the player if they teleport to a different location on the stage and thus increase their distance from the enemies to over 1.5. Make sure to connect up the chasePlayer transition back to the Chasing Player state to accomplish this.

 

Now go back to the Chasing Player state and add a Trigger Event action, which detects collision with a trigger. When we created the enemy we gave it a capsule collider so that it could interact with other colliders in the scene, and in this the case the collider we are looking for that is also a trigger is the bear model inside the bullet. The bear is the only trigger in the scene, so whenever a trigger event occurs it will definitely be a collision between the enemy and one of the player’s bullets. Make sure to select On Trigger Stay as the type of trigger, as this detects a collision when the bullet and enemy overlap for some amount of time. This is automatically triggered when they collide for the first time, but is also triggered if the player is close enough to put their gun inside the enemy model and fire it, which would also count as a hit to the enemy. The latter would not occur if we just detected a collision entry, rather than a collision stay, so On Trigger Stay is the setting we want to use. Finally, have the Trigger Event send a “die” event, which we will use to to transition to the enemy’s dead state. Therefore also create a “Dead” state and add a transition from “die” to this state.

Now add an identical Trigger Event action with exactly the same settings to the Reached Player state, since it is possible for the player to shoot the enemy in this state as well. Add a “die” transition event to the Dead state from this state as well.

Now click on the Dead state and add a Find Game Object action to find the enemyCounter and store it in an identically named variable. Then perform the Send Event action on the enemyCounter variable with the event subtractEnemy, which is a global event that we created earlier on in the project. These two actions combined will locate the enemyCounter object, which is constantly monitoring the number of enemies on screen, and will send it a message to subtract one from the enemy total, since one just died.

Then add a Destroy Component action and select the Use Owner’s Capsule Collider from the UnityEngine drop-down menu. Removing the capsule collider stops the enemy’s body from impeding the flight of any bullets the player shoots close-by to the enemy character model.

Finally, add a Wait action in the Dead state that waits for 10 seconds and then sends an event called destroySelf, checking the Real Time box in the process. Connect this transition up to a new state called Destroying Self.

In the Destroying Self state create a single Destroy Self action and make sure to leave Detach Children unselected, as we want any children to remain attached to the enemy and be destroyed from the game along with it.

Now we are going to create a prefab for the enemy object. Firstly, drag the Enemy object from the Scene Hierarchy directly into the myPrefabs folder. Then delete the original game object from the Scene Hierarchy as shown in the image below.

Now select the enemySpawner object and navigate to the Spawning Enemy state in its FSM. Inside the Create Object action that should have a red error icon on it, drag the newly created Enemy prefab into the Game Object field to quiet the warning and complete the spawner’s FSM.

PlayMaker allows users to directly edit the FSMs of prefabs, so if we need to edit the Enemy prefab again, this can simply be done by selecting it inside the myPrefabs folder and then performing the usual FSM edits.

Now that the main logic for the enemy girl is complete, we will work on animating the girl. Select the Enemy’s girl child object from the Scene Hierarchy and then click on Window and then Animator from the drop-down menu to display the girl’s animation controller. The SapphiArtchan model we are using comes with various animations already, so all we have to do to animate the model in-game are select the animations we want and make some tiny edits.

The first of these edits is to set the exit time of the idle-running transitions arrows to 0. Select each of the two arrows in turn and uncheck the Has Exit Time box in the Inspector window.

Then repeat this for the running, hit01 and KO_big animations, all of which we will be using in the game.

This animation system works by setting booleans, or true or false values, to certain values to cause a transition between state. For example, being in the idle state and setting the transition boolean to running to true causes the character model to switch to the running animation. This is all the specific detail we need to actually use this animation controller in playMaker, so we will not go any further in depth into animation in this tutorial.

 

Now go back to the Chasing Player state in the Enemy prefab’s FSM and create two Set Animator Bool actions. The first one should set the param_idletohit01to false (indicated by an unchecked Value box) and the second should set the param_idletorunning to true (indicated by a checked Value box). This will stop the enemy from attacking the player, if they are currently attacking them (otherwise nothing happens) and cause them to start running. We need the first parameter to be set to false because we can reach this state from the Setup state or the Reached Player state, in which the enemy is attacking the player, which is an animation we no longer want in the chasing state.

 

Now go to the Reached Player state in the Enemy prefab’s FSM and create more two Set Animator Bool actions. The first one should set the param_idletorunning to false and the second should set the param_idletohit01 to true. This will stop the running animation and cause the enemy to start attacking the player (by kicking them), when they reach them.

Now go to the dead state and set the hit animation to false, the run animation to false, and the KO animation, param_idletoko_big to true. This will stop any other animations and cause the enemy to enter the KO state in which they lie down on the floor.

We are now finished with the enemy prefab and can move on to creating the game’s music.