(Devlog 8) Player Animation and Headlamp Overhaul


Player Animation and Headlamp Overhaul

The devlog explains how I remade the headlamp asset and player animation. I added directional based player animation to my character meaning that the character faces the direction it moves in. I decided to add this as I believe it makes the game look a lot more polished.


Player Animation

I added directional based animation to my player, the movement animation now consists of a walking left, right, up and down animation. This was important to the game as the player is one of the most important visual art pieces the player sees when playing the game.

To use multiple different animations inside the movement I had to  tweak my  player movement script slightly to accommodate the script detecting the players horizontal and vertical movement. The video I followed to implement this was Brackey's "2D Movement in Unity (Tutorial)". This tutorial gave me all the information necessary to add this function into my game. 

Headlamp Overhaul

Because I added directional based movement animation to my character, this opened up an opportunity to polish the look of my headlamp. I decided to my a directional headlamp light instead of just illuminating a radius around my character. The way I did this was editing the circumference of the 2d spotlight component so it only shines in one direction. To make the light shine in the direction the player is facing I made a script that handles the rotation of the light based on player input, I added this script to the player.

  


Challenge and Solution

Adding directional based movement animation and changing the headlamp posed a really difficult problem, because adding these two new functions, it makes the game appear slightly more 3 dimensional. The reason for this is in real life, a headlamp is supposed to be attached to someone's forehead right? which is the same in my game. However, because of this, when the character is facing away from the camera so the characters forehead is hidden, the light needs to be behind the character to give the illusion the light is coming from there forehead. Which is fairly easy to fix, all you need to do is set the players sorting layer to a different layer then what the light is on. But it is not that easy, because now the light is on a layer behind the character, which means  when the character has turned around so that you can see the front, now the light is behind the character, which obviously the light coming from a headlamp on the characters forehead should not be illuminating behind the character.


As you can see from the image above, when the character is facing the camera the light is behind the him and when the  character is facing away from the camera the light is also behind the him. When the character is facing away from the camera it is fine, this is what we want, it gives the illusion the light source is coming from the character headlamp attached to his forehead. However, obviously when the player is facing the camera the light is still behind the player which is not what we want. And if you invert this by setting the light on the same layer as the player is on then it just swaps problems, the light will be coming from the back of the characters head.

After hours of trying to fix this and researching  solutions I came to the understanding I was not able to fix this in a conventional manner, I was going to need to implement possibly the dumbest solution to this problem. That being a second light head, this second headlamp is set to ONLY illuminate the specific layer the player is on, so in simpler terms I would have 2 lights, one light would illuminate the ground and the other light would illuminate only the player and write a script so the second light that only illuminates the player gets toggled on when the player is facing the camera.



The images above show the two headlamps and the script that detects when the player is facing the camera and toggles the headlamp that only illuminates the players sprite.

Leave a comment

Log in with itch.io to leave a comment.