Player Damage System in Unity

Rusben Guzman
2 min readMay 19, 2021

The objective in this example is to make the player lose a life when he touches an enemy. In order to do that the first step is to make sure both GameObjects contain a Collider component, we have to check the “Is Trigger” field in both colliders and add a rigidbody to the enemy.

Then we are going to assign a tag “Player” to the GameObject Player.

In the player script we are gonna create a variable for the lives and create a damage function to decrease the player lives and check if the player died.

In the Enemy script we have to Use the OnTriggerEnter2D() function to detect the collision, and if the enemy collides with the player we have to call the Damage function in the player script. To call the damage function we’re gonna use other.GetComponent<Player>() to get the player script reference from the Player.

And we are good to go, now we have a functional Player Damage System in Unity.

--

--

Rusben Guzman

A Software Engineer passionate about game dev and interactive products with Unity. I consider video games to be the artistic expression of programming.