2D Mobile: Setting Up The Player

I finished setting up the player with animations and movement!

Player Setup

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.

Movement

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.

Jumping

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;
}
else
{
_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;
}

In FixedUpdate:

if (_enableJump)
{
_rb2D.velocity = new Vector3(_rb2D.velocity.x, _jumpHeight);
_enableJump = false;
}

This will allow me to jump with a jump height.

Player Animation

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()
{
_anim.SetTrigger(“attack”);
_swordAnim.SetTrigger(“swordAnimation”);
}

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)
{
StartCoroutine(AttackRoutine());
}

The coroutine calls the Attack method from the player animation.

private IEnumerator AttackRoutine()
{
_isAttacking = true;
_anim.Attack();
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.

Camera Setup

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.

Conclusion

With everything set, the player controller works nice and smooth as intended!

Gabriel

--

--

--

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Newsletter, 7th of September

Setting up your NAS Box, 2021 Edition, Part 3

How to automatically extract EXIF metadata from photos in Airtable without code

Bias in algorithms

PrintCSS: Counter and Cross References

The result of the above HTML and CSS code

Quickly Set Up an LDAP Server Using Vagrant

What makes a Full-Stack Developer and how to become one over 1 weekend

Criteria for TRONSR ranking

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gabriel Perez

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

More from Medium

Installing Unity & getting started with game Development

How to animate and move a simple player with Mixamo and Unity/C#

Saving Data in Unity3D Using Files

2D Mobile: IDamageable Interface