2D Mobile: Setting Up The Player
I finished setting up the player with animations and movement!
The goal of setting up the player is to have horizontal movement, input for jumping and attacking, animations, and setting up a camera that will follow the player.
The movement is always the easiest part. I used the velocity of the player’s rigidbody with a reference to the horizontal input and speed in FixedUpdate.
float xMove = Input.GetAxisRaw(“Horizontal”);
_rb2D.velocity = new Vector3(xMove * _speed, _rb2D.velocity.y, 0);
I store the input of the horizontal movement into a variable called xMove. I then take the player’s rigidbody velocity and assign a new Vector3 by multiplying the xMove with the speed for the x-axis, and keeping the y axis as its current y position.
To make the player jump, I created a Grounding script to check if the player is grounded or not.
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, _groundCheckDistance, _layerMask);
Debug.DrawRay(transform.position, Vector2.down * _groundCheckDistance, Color.red);if (hit.collider != null)
_isGrounded = true;
_isGrounded = false;
If the raycast hit’s a collider in the Ground layer, then is grounded is true. Else it’s false. I created a method that returns if the player is grounded. It will be called in the player class for the logic of jump.
In the player class, if is grounded equals true and I hit the space key, I want to set the _enableJumping to true. Because I am using a physics-based movement, I don’t want to use input in FixedUpdate. So I have to create a bool that acts as an enabler. I use this bool in Update once an input has been detected so that it can trigger the movement in FixedUpdate.
//Enable Jumping when hitting the space key
if (Input.GetKeyDown(KeyCode.Space) && _isGrounded)
_enableJump = true;
_rb2D.velocity = new Vector3(_rb2D.velocity.x, _jumpHeight);
_enableJump = false;
This will allow me to jump with a jump height.
I created a new class for the player animations. I want to create methods for each animation state. One for moving, jumping, and attacking. I can then call these methods within the player class at their appropriate logical location.
I also set the animations in the Animator panel with transitional conditions.
For example, if I want to trigger the attack animation which has an “attack” trigger on the Idle and Run state, I call the Attack method from the player animation class:
public void Attack()
In the player class, when I hit the left mouse button, I call a coroutine that handles the attack.
if (Input.GetKeyDown(KeyCode.Mouse0) && _isGrounded)
The coroutine calls the Attack method from the player animation.
private IEnumerator AttackRoutine()
_isAttacking = true;
yield return new WaitForSeconds(0.47f);
_isAttacking = false;
I want to handle the state the bool is in so that when I attack, I have no control over my movement. Hence why a coroutine is used to stop my movement for almost half a second.
For the camera setup, I used Cinemachine. I installed cinemachine from the package manager and create a virtual camera. In the virtual camera settings, I set the target to the player. That is all! Simple and easy to use.
With everything set, the player controller works nice and smooth as intended!