Week 9 - Enemies and Interaction


KIT109 – Enemies and Interaction DevLog

For this week’s DevLog, it is showcasing the Enemies and Interaction update to the game.

For this update, I implemented the pickup and enemy function of the game. To start off with used bits and pieces from DestroyOnCollision scripts from class practicals as a starting point to destroy the stars as the player collects them. I needed to add this script to each individual start for this effect to work as intended. I made use of the OnColliderEnter2D function so that when the player touches the star, it will be destroyed. I also required this same function so that when the star gets destroyed, it increments to the score so that the player knows how many stars they have collected. I added some more stars to the level to create a bit more of a challenge for the player.

 

To set up the score correctly, I needed to add just a basic UI to showcase the score in text format so that the user knows how many stars they have. To do this I used the PlayerCollectScore script from a class practical to achieve this. I needed to edit the script in order to accommodate for the different names of the objects. I gave all the stars a new “Star” tag so that I could use this tag in code to increment the score every time one gets destroyed. I also needed to update the reference to the score by creating an empty text box to hold the score value so that it would update every time a star was collected. I also added an image of a star just for looks next to the blank text box to show the user what they are collecting.

 

Collection of stars and incrementation of score

 

 

The next step was getting the enemies to be interactive. I needed to create their animations, so they appear they are moving and not stagnant. First, I needed to create their animations by creating a new animation controller as well as each animation for moving left and right. I created a new blend tree for the enemies to get their individual movements working as intended. I needed to write some code in order to get them moving correctly. I needed to give them a starting and an end position so that they move back and forth along the platform. I also needed a speed and a distance for the enemy to walk. I changed the enemy’s position based of the speed and direction it was facing. If the enemy reaches the distance the direction gets changed and moves the other way and this goes on for forever.

 

Enemy movement in the game

 

For the game to function correctly, I needed for the player to die if they touch an enemy. To achieve this, I also used a version of DestroyOnCollision from previous practicals. I needed to edit the script so that it would only apply to the player and not objects such as the stars and the platforms around it. I set up the player with a tag so that the method will only be called if the enemy is touching the player. So, the player could collect the stars and if they touched the enemy, they would die, and they could not respawn so they would have to restart the game. I wanted the game to restart by itself if the player touched the enemy. In order to achieve this, I needed to a bit of research on how to reset the game state to when it first started. From some of the practicals, I have used Scene Switcher. Along these lines I found out that I can reload a scene by using SceneManager.LoadScene. By simply calling this and using the specific scene name it will just reset the state of the level back to when the game started when the player touches an enemy.


I also needed an animation for when the player dies. With the player sprite, it came with many animations and the death animation was one of them. Like all the other animations I needed to put the specific animations in the correct order for the way that the player is facing and add this to the blend tree. The death animation at first would not work as the game reset too quick so I needed to edit the code so that it would delay the reset to play the animation. This required a coroutine so that I could use WaitForSeconds to delay the reset enough for the animation to play. I also needed to be able to freeze the player so that they could not move when the dying animation is played which also required a change to the code.


Player death animation and game reset

 


I was unable to acquire feedback from my peers this week as I did not finish implementing the updates during or leading up to the tutorial. Some things still to add to get the game functioning correctly is a lives system so that it will keep resetting the player until the player runs out of lives. Once the lives system is function correctly, the level will be pretty much complete apart from the UI side of things. Overall, I happy with how things are coming together.

Leave a comment

Log in with itch.io to leave a comment.