Simple platformer controls for 2D graphics in Unity
Let’s go over another take on platformer controls. This time for 2D graphics.
First, create an empty game object and set the name to Player. Drag in the first frame of the idle animation to the hierarchy and add as a child of the player object. Reset the position of the child object:
On the parent object add a Ridgidbody2D and a Box Collider 2D. Freeze the rotation on Z:
Create the Idle, Run and Jump animations:
Open the Animator window and add a speed and jump parameter. Set up the transitions between idle, run and jump:
Create a script called Player and add to the parent object:
Open up the Player script and add:
-Rigidbody2D for the rigidbody component on the player
-Two serialized floats: one for the jump height and the other for the movement speed
-Animator for the animator component on the child object
-Bool for checking if the player is on the ground
-Transform for the transform component on the parent object
Initialise Rigidbody2D, Animator and Transform in the Start function:
Create the functions for checking if the player is standing on the ground or jumping. For this script we use ray to check if the player is grounded:
Lastly, create the movement function. You can rotate the player either via the local Euler angles or the local scale:
Good luck!