Devlog 3: Enemies and Interaction


This checkpoint is about implementing enemies and interactions. In this third devlog, I will cover implementing enemies, player health, and interactions around those two aspects.

Avoiding enemies is one of the main challenges of the game. The desired enemy functionality was for the enemy to patrol an area and deal damage to the player if they contacted. Each enemy would have two positions in world space which they move to and from. To implement this, an enemy has two child game objects to either side of the enemy body. The horizontal positions of these objects in world space are recorded and used as the patrol limits. The enemies are made to randomly move in a direction at the start, and change if they reach the limit positions.

An enemy in the scene view with the patrol limit objects shown as circles

Although the player will not be able to kill the enemies, the enemies will be able to kill the player. This requires player health which can be depleted by enemies. Player health is implemented by a new script with a health number and setter functions that can be called from other scripts. When the enemy collider is in a collision with the player collider, the player health setter method will be called to damage the player. So that the player doesn’t instantly die, a timer that decrements each physics update is used to limit the amount of times damage is dealt. Player health is displayed through a health bar at the bottom of the screen which is updated by the health setter functions. The implementation of the health potions was the opposite of the enemy: a stationary object that when in a collision with the player collider, increases the player health and then destroys itself. Alternatively, I could have checked collisions on the player by game objects and increased (for health potion) or decreased (for enemy) player health depending on what game object the player collided with.


The player losing and gaining health

Files

Week 12 Friday.zip Play in browser
Oct 05, 2023