Mobile Game Progression

Player collision with Enemies

One of the glaring issues while playing my 2D mobile game is that our player doesn’t make any contact without enemies, and when the enemy does make contact with the player, the enemies get stunned while the player is still able to move around. It is time to change that now and have both enemy and player get stunned to make it more realistic.

Creating the Collision Animation:

Thanks to filebase we are provided with the player hit sprite sheet therefore we won’t need to be doing any custom animating. Slice and create a new animation for the player and named it “Stunned”.

Next within the animator we are going to transition the “Stunned” state from the “Any State”.

We are also going to create a new parameter trigger called “is Stunned” and set that as our Stunned condition.

Next before we can start scripting, if you haven’t already, create a new tag type called “Enemies”, this will help with identifying when the player collides with enemies.

Scripting the Player interaction:

First we are going to create one new variable within the Player script called “is Stunned”, and by default it should be set to false. Then we are going to create a new on Trigger Enter 2D method which checks whenever the player has collided with an object with the tag called “Enemy”.

When they do make contact, several things is going to happen; First we are going to turn off Dashing just in case the player was dashing when the contact was made. Then we are going to turn on our bool for “is Stunned” and set the player movement speed to zero. Then we are going to gain access to our animation from the player Animation script (which we will create later) along with a Coroutine.

We need to create a new Coroutine to control two factors, the returning of the player speed and also turning the bool “is Stunned” back to false.

The next modification is going to be within the Attack method, we only want to be able to attack when the player is not stunned, therefore we need to set that as a condition for the attack script.

In the player animation script we are basically going to create a new public method which toggles the trigger parameter within the animator so the stunned animation is played.

Back in Unity we will see that whenever we collide with any enemy with the tag called “Enemy” the stunned animation will trigger and the attack function will be locked out until the coroutine is finished.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store