Deeper Dive Inquiry Post #2 – Unity (Playable Character)
Introduction
In my first post, I set the goal of creating a 2D platformer. After exploring the Unity interface, I moved into the Action phase. My goal for this stage was to create a controllable character in Unity. One that could move left and right and jump.
I used the following video by “Chris’ Tutorials” to help create my character:
The Player Setup
- Component Architecture: I learned how to use Rigidbody2D to give my player mass and gravity, and BoxCollider2D to prevent them from falling through the floor.
- Input Handling: I wrote a C# script to listen for player input. I chose to use the Input.GetAxis(“Horizontal”) method, which allows for smoother movement compared to hard-coded key presses.
- The Ground Check Logic: One challenge I hit was infinite jumping (the player jumping while mid-air) because I wasn’t checking if the player was on the ground before allowing them to jump. I had to learn about Raycasting and LayerMasks to ensure the player is actually touching the ground before allowing a jump.
- I added animations to the player so he would have a different animation when he was walking vs idle vs jumping. All of the animations came in the sprite package I downloaded from the Unity Asset Store.
Here is a video of the character I created: