Today, I have gained the knowledge of Finite State Machine Design Pattern and I will be posting an article specified to FSM in the following days. Going forward on the Third Person Shooter Game, I have added a blood effect today.
The enemy will be attacking to the Player according to a trigger collision. Enemy game object already has a Character Controller (and collider of course) attached. A Sphere Collider has been added and Trigger is set to true. This is a better approach than continously measuring the difference between the player and deciding on the action according to the distance — this requires less workload from the device for sure.
Once the enemy collider is triggered by the player, Enemy switches to Attack state. And vice-versa on the trigger exit.
Additionally, I have added a blood detail once the enemy is shot. The effect I will be using is a taken from Filebase.
Player shooting is handled in a seperate script called “Shoot”. Here’s the method that is being called within Update method:
However, you will realize that the bloods are being instantiated at the Sphere Collider:
The enemy has 2 colliders (neglect the glasses :) ) and the one on the outside needs to be ignored from the ray sent from the player.
The solution is to create a child object on the Enemy game object and attach the Sphere Collider to the child object and set the Layer of the child object to a different Layer which will be ignored. Create a new Layer called EnemyTrigger and note it’s layer ID. Using it’s ID, we will tell the Raycast to ignore it — Just as I have done in the 2.5D Platformer game.
Set the child object’s layer to Attack Trigger. If you want to, you can create a new layer for the Enemy also (make sure that you do not change the child object’s layer to Enemy). If you dive deep into Physics.Raycast method, you can realize that we can include a layerMask within. The specified integer value simply tells that the Ray is going to detect the collisions at that specific layer. Certainly, by adding and sign (|) we can define multiple layers to detect.
Updating the Shoot Script:
Now, it works fine! I have also added a line to destroy the bloods after 1.5 seconds.
Lastly, make sure to set the Character Controller Radius in line with the visual.
And that’s all :).