The goal for this article series is to use PlayMaker in Unity to create a FPS shooting game, and export it to the STYLY editor.
There are 7 articles for this series, so if you roughly look through the series first, I think you will get the general flow of the tutorial. In this article, we will learn how to animate the enemy girl.
 Setting up the enemy characters
Create an empty Game Object name “Enemy” in the Scene Hierarchy.
Drag the SapphireArtchan prefab in your myUnityAssets folder onto Enemy, to make it a child object as in the image above.
Rename SapphiArtchan to “girl” for simplicity.
As in the image above, add a Capsule Collider component to the Enemy object from the inspector window, and edit it so that it fits around the enemy’s body. You can click the Edit Collider button to change the size of the editor by clicking and dragging.
Add a rigid body to the enemy, so that physic laws are applied.
Also, check the Use Gravity and Is Kinematic box. The Is Kinematic setting will make the enemy able to collide with objects such as bullets. But, no force except gravity will be applied to the enemy in the game.
Next, click the gear icon next to the C# script named Sapphi Art Chan_Anim, and choose Remove Component. C# scripts are not compatible with STYLY, so we need to delete it.
To make the enemy appear at a natural height in the game, change the Y value of the girl to -1.
Create a new FSM in Enemy with a Setup state. Store the main camera as a game object named “mainCamera”, and set “girl” for the Child Name.
(Since we need to refer to both the enemy and the main camera from a FSM in after steps, it is better to store them as variables here)
Create a new state named “Chasing Player”, and create a FINISHED transition from the Setup state. Then create a “reachedPlayer” event, and add it to the Chasing Player state.
As in the image below, add a Smooth Look At action and move Towards action to the Chasing Player state.
The Smooth Look At action will make the enemy look towards the main camera, thus towards the player in VR. Move Towards will make the enemy move towards the player, and this will be the key setting of this game. Set the Finish Distance to 5, so the enemy girl will stop at an appropriate distance from the player, and when the movement is complete, the reachedPlayer event will get triggered.
We need to add a branch transition to Reached Player before the reachedPlayer is triggered.
Create a new state named reached Player, and add a transition to reachedPlayer. In this Reached Player state, add a Get Distance action to get the distance between the enemy (Use Owner) and the main camera(Current position of the player). Set it so that it is active for every frame. After that, create a Float Compare action, in where a transition to chasePlayer occurs when the distance is larger than 1.5 (When Float 1 is larger than Float2). With this setting, if the player teleports to a different position and the distance between the enemy gets larger than 1.5, the enemy will start chasing the player again. Don’t forget to create a transition from chasePlayer to the Chasing Player state.
Then, return to the Chasing Player state and add a Trigger Event action. This is going to be used to detect when a collision occurs.
When we made the enemy, we created a capsule collider to enable it collide with other colliders of objects. The collider here is a trigger, and is also the bear model in the bullet.
This bear is the only trigger in the scene, and every time a trigger event occurs, a collision between the enemy and the player’s bullet will happen. Set On trigger Stay as the trigger type,and judge it as a collision when the bullet and the enemy touches each other for a certain amount of time. This will be triggered when the enemy and the bullet collides for the first time,and also when the player shoots the bullet in close range, and the bullet immediately collides with the enemy. The later will not be detected with collision entry, so the setting we want to use for this program will be Trigger stay.
So, create a trigger event in Chasing Player using On Trigger Stay. Set it so that it sends a “die” event which transitions to a state called “Dead”.
In the Reached Player state, create a Trigger Event action with the exact same settings. This is because there is the possibility that the player will shoot the enemy in this state too. Also,create a die transition to the Dead state.
Select the Dead state, find the enemyCounter, and add a Find Game Object action to store the value to a variable with the same name. Set the Send Event as subtractEnemy, and run the Send Event action in the enemyCounter variable. This is the global event we created in previous steps.
By combining these 2 actions, the enemyCounter object will be checking the number of enemies constantly, and when a enemy dies, a message to subtract 1 from the enemy count will be sent.
Add a Destroy Component action, and choose Use Owner’s Capsule Collider from the UnityEngine dropdown menu. By removing the capsule collider, the bullets that the player shoots from a near distance will not effect the enemy models.
Finally, add a Wait action to the Dead state, set the wait time to 10, and upon Finish Event, send a destroySelf event. Don’t forget to check the Real Time box. Create a transition to a state named “Destroying Self”.
Create a Destroy Self action in the Destroying Self state, and uncheck Detach Children. This setting is for deleting the enemy with the child aspect attached.
Next, create a prefab of the enemy object. Drag and drop the enemy object from the Scene Hierarchy to the myPrefabs folder. Then, delete the game object from the Scene Hierarchy.
Click the enemySpawner object, and then click the Spawning Enemy state in the FSM. Find the Create Object action with the red notification mark, drag and drop the Enemy prefab to the Game Object field, remove the error notification, and your spawner FSM is complete.
In playMaker, users can directly edit the FSM’s of prefabs. If you need to edit the Enemy prefab afterwards, you can select it from the myPrefabs folder, and edit it as you have done in previous steps.
We’re done with the logics needed for the enemy character, so lets start animating her.
Select the enemy girl object in the Scene Hierarchy, click Window > Animator from the dropdown menu, to display the animation controller of the enemy.
The SapphiArthchan model we are using in this tutorial has many animations already set, so to add animations, all we have to do is select the animation we want and adjust the settings.
The first thing to do is to select the arrow going from idle to running (so that it turns blue), and set it to 0 by in-checking “Has Exit Time”. Do this for both arrows.
Repeat this action for the “running”, “hit01”, “KO_big” actions. We will use these later in the game.
In this animation system, the state transitions are booleans, so they change by true or false. For example, if the current state is idle, and the transition to running becomes true, the character model performs the running animation. (This information is needed to understand how to use the animation controller in playMaker, but I will not explain further details in this tutorial)
Return to the Chasing Player state in the Enemy prefab FSM. Create 2 Set Animator Bool actions. For one of them, set the parameter to param_idletohit01to and the value to false, and for the other, set the parameter to param_idletorunning and the value to true.
With this setting, when the enemy is attacking the player, the enemy will stop attacking and start running. This state can be reached from the Setup state or the Reached Player state, so we need to set the first parameter to false. This is because the animation is for attacking, but when chasing the player, we don’t need the animation to be active.
Select the Reached Player state in the FSM of the enemy prefab, and create 2 Set Animator Bool actions. For one of them, set the parameter to param_idletorunning and the value to false, and for the other, set the parameter to param_idletohit01 and the value to true. The enemy will stop running when she reaches the player, and starts kicking and attacking the player.
Select the Dead state, and set the hit animation to false, run animation to false, KO animation to true, and param_idletoko_big to true. With these settings, the enemy will reach the KO state, and drop to the ground.
That is all for the enemy prefab. In the next article, lets set the music for the game.[Related Articles]
・Using PlayMaker to create a game, and exporting it to STYLY -1-