Player Movement


This week I finished coding the player's movement and wall/floor interactions. The player can now run, jump, wall slide and wall jump. The code is also laid out in such a way that will make inserting animations later on nice and straightforward.

For the collision system I’m just using unity’s inbuilt physics system with a rigidbody2D component on the player and collider components on it and the walls/floor. 

To detect if the player is on the floor so that they can’t just jump in mid-air, I’m using an empty child object of the player’s that I’ve called Ground Check and manually placed at the bottom of the player’s hitbox, and the player detects if an object in the ground or wall layers is within a small radius of Ground Check. The reason I’ve checked against both layers is that I will probably want to have objects in the future that act as walls that I can slide on, as well as stand on top of and jump from. 

I’ve used a very similar approach for detecting a wall that a player can slide on and jump off, I’ve got another child game object called Wall Check positioned to the very right of the player’s hitbox (the movement script makes the player scale flip so Wall Check is always in front of the player) and the player movement script detects if anything on the wall layer is within a small radius of Wall Check. 

For the movement itself the horizontal movement uses unity’s built in input system and settings to get the horizontal axis as a float value, then set the player’s velocity proportional to that and the player’s speed variable. For the jumping it simply detects if the w key is pressed and adds an upwards velocity to the player if the player is standing on the ground, but it also adds extra height to the jump of the w key is held rather than just pressed.

I got a lot of help from a YouTube tutorial by Bendux on how to handle the wall and floor detections, as I was a bit confused at first, but his 2D player movement tutorial was really helpful and informative in solving these issues.

I received some very positive feedback from my peers, saying that the controls feel nice and responsive, which is what I was going for. I would however like to further tweak these values to get it feeling just right, but that’s easy done through the unity inspector window.

Below is a gif showcasing the movement of the player and its interactions with the walls/floor.


Files

Build.zip Play in browser
Apr 26, 2024

Leave a comment

Log in with itch.io to leave a comment.