top of page

Week 6 - Fixing states transitions and implementing a scoring UI

This week I put a focus on fixing the enemy behavioural states. When the enemies were fleeing the player, the position was not updating. The flee state additionally addressed all enemies regardless of their individual position. This meant that if a single enemy was out of the players set range while other enemies were still fleeing, it would freeze and not move. In this case, the enemy’s desired movement was to revert back to the wander waypoints and switch states independently.

To address the flee position update issue, I used the ‘distance’ function within unity to calculate the distance between enemies and the destination. The destination position was updated if an enemy came within a certain proximity to the given destination while ‘fleeing’ was set to true.

I resolved the state switch issue by creating a temporary wander destination variable which held the last wander waypoint destination before the fleeing function was activated. If any enemies were outside of the players range, they would return to this wander destination which would then re-activate the wander function for that individual enemy.

Additionally, if the enemies were fleeing towards the bounds of the map, a destination point was chosen to the right or left of the player, meaning that they did not get stuck when they reached the bounds.

Now that I had resolved the enemy movement issues, I focused on creating a points system. The plan was to create an animated floating score above the enemy, which would activate when the player interacted with the enemy.

I did not initially understand that I had to create and attach a canvas to the enemy and set it as the parent of the prefab for it to work. I figured this out after watching video tutorials and researching through the Unity forums.

To start, I created a canvas and attached it to the enemy prefab, setting the size to fit the floating text.

I then created a script which took the position of the main camera and rotated the canvas in the direction of the camera using Unity’s ‘lookat’ function. This meant that the floating points were always visible to the player.

I created a textbox which I would use to reference from within the enemy base class script and animate using the animator. Code of the score function can be seen below:

Using Unity’s animator, I created the floating point animation and set it to change colour through its animation lifecycle. In order to activate this animation, I created a new trigger, calling it ‘hit’ and attached the floating point animation to it.

I then initialised the function within the ‘destroy’ function (which is called in the enemy update base class). However, enemies were being destroyed too quickly, meaning that the point system animation was cancelled before finishing. To solve this, I created a Coroutene for the ‘destroy’ function. Within this Coroutene, the initial animations and functions were instantiated, but the enemy object itself was only destroyed after 2 seconds – giving the animations enough time to run before the enemy object was destroyed.

I have achieved more this week than originally planned. The task I set myself for next week was to create an enemy attack behavioural state.

However, I believe this is not as much of a priority as originally thought. I will instead move to the tasks planned for the week after next which consists of creating a player attack motion. This will be in form of a lunge. I will use this to balance the number of enemies the player can attack within a certain time frame.

I will additionally add an energy bar which will deplete over time. Enemies killed will keep the player’s energy up.


Recent Posts
bottom of page