This is the 3rd and final installment in the mini tute series ‘Creating 2D Animation States in Unity3D’ . You can find previous installments here: Part1  | Part 2

In part 2 we created our Player animations, created an animation controller, organised our animations in the Animator window, and then mapped them to ‘Conditions’ based on animation ‘Parameters’.
Now we will finish it off by stepping onto the coding dance floor, and creating a system for switching between animations using the keyboard. Complete Project source file link can be found at the end of the tutorial.

Note : Before we get stuck in, I would like to take a moment to do some quick edits to our existing projects from Part 2

  • In the Project window, select the ken_crouch animation, and in the inspector, make sure ‘Loop Time’ is unchecked. this will stop the ken from crouching and standing up continuously.
  • In The Animator window, create a new transition from animations ken_crouch to ken_walk, with the mapped condition ‘state Equals 1’

 

Add a Background Graphic to the Scene

Lets add an appropriate Background to our scene, this will give us a reference point for where the floor should be.

In your Project window, expand the ken-sprite-sheet in your spritesheets folder, and find ‘ken-sprite-sheet_49’  which is a background graphic. Drag it into your scene window and position it at the coordinates 0, 0, 0.
You may notice your player is no longer visible, this is because the the Background is sitting above the player (closer to the camera). Select your Player and change the Z position to -1.

Now would be a good time to fix the games screen resolution. Select 16:9 from the resolution drop down, as it’s a good match to our background graphic’s aspect ratio.

UnityStates3_2

 

 

 

 

 

 

 

Note : Resolution may change from project to project depending on the devices requirements, 16:9 is just an example used for this tutorial.

 

You may find the background is now slightly smaller than the viewable area of the camera, select the background element and increase the scale slightly so it completely fills the camera, as well as changing the positions until it fits.

UnityStates3_3

Creating the Player Controller C# Script

If you have followed on from the previous tutorial, you should have an empty ‘scripts’ folder in your project root, if not, create it (right click project tab window > Create > Folder ).
Lets create a new C# script inside this folder by right clicking the folder in the project window and selecting Create > C# Script.




Rename it PlayerController and drag the script onto your Players Inspector window to add it to the player.

UnityStates3_4

Open the script in MonoDevelop by double clicking it (your welcome to use any code editor, for this tute i’ll be using Mono).

Note : This tutorials focus is not on teaching coding practices, the code offered is designed to demonstrate the features of the animation controller in a simple way.  I decided instead of breaking the code down and explaining each little thing, that it would be better to just dump it on you with comments to let you play around, as I feel this is the best way to learn!
Copy and paste the code below into your Player Controller, then attach it to you player object by dragging it onto your Player objects Inspector window.


using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	public float walkSpeed = 1; // player left right walk speed
	private bool _isGrounded = true; // is player on the ground?

	Animator animator;

	//some flags to check when certain animations are playing
	bool _isPlaying_crouch = false;
	bool _isPlaying_walk = false;
	bool _isPlaying_hadooken = false;

	//animation states - the values in the animator conditions
	const int STATE_IDLE = 0;
	const int STATE_WALK = 1;
	const int STATE_CROUCH = 2;
	const int STATE_JUMP = 3;
	const int STATE_HADOOKEN = 4;

	string _currentDirection = "left";
	int _currentAnimationState = STATE_IDLE;

	// Use this for initialization
	void Start()
	{
		//define the animator attached to the player
		animator = this.GetComponent<Animator>();
	}

	// FixedUpdate is used insead of Update to better handle the physics based jump
	void FixedUpdate()
	{
		//Check for keyboard input
		if (Input.GetKeyDown (KeyCode.Space))
		{
			changeState (STATE_HADOOKEN);
		}
		else if (Input.GetKey ("up") && !_isPlaying_hadooken && !_isPlaying_crouch)
		{
			if(_isGrounded)
			{
				_isGrounded = false;
               //simple jump code using unity physics
               rigidbody2D.AddForce(new Vector2(0, 250));
				changeState(STATE_JUMP);
			}
		}
		else if (Input.GetKey ("down"))
		{
			changeState(STATE_CROUCH);
		}
		else if (Input.GetKey ("right") && !_isPlaying_hadooken )
		{
			changeDirection ("right");
			transform.Translate(Vector3.left * walkSpeed * Time.deltaTime);

			if(_isGrounded)
			changeState(STATE_WALK);

		}
		else if (Input.GetKey ("left") && !_isPlaying_hadooken)
		{
			changeDirection ("left");
			transform.Translate(Vector3.left * walkSpeed * Time.deltaTime);

			if(_isGrounded)
			changeState(STATE_WALK);

		}
		else
		{
			if(_isGrounded)
			changeState(STATE_IDLE);
		}

		//check if crouch animation is playing
		if (animator.GetCurrentAnimatorStateInfo(0).IsName("ken_crouch"))
			_isPlaying_crouch = true;
		else
			_isPlaying_crouch = false;

		//check if hadooken animation is playing
		if (animator.GetCurrentAnimatorStateInfo(0).IsName("ken_hadooken"))
			_isPlaying_hadooken = true;
		else
			_isPlaying_hadooken = false;

		//check if strafe animation is playing
		if (animator.GetCurrentAnimatorStateInfo(0).IsName("ken_walk"))
			_isPlaying_walk = true;
		else
			_isPlaying_walk = false;

	}

	//--------------------------------------
	// Change the players animation state
	//--------------------------------------
	void changeState(int state){

		if (_currentAnimationState == state)
		return;

		switch (state) {

		case STATE_WALK:
			animator.SetInteger ("state", STATE_WALK);
			break;

		case STATE_CROUCH:
			animator.SetInteger ("state", STATE_CROUCH);
			break;

		case STATE_JUMP:
			animator.SetInteger ("state", STATE_JUMP);
			break;

		case STATE_IDLE:
			animator.SetInteger ("state", STATE_IDLE);
			break;

		case STATE_HADOOKEN:
			animator.SetInteger ("state", STATE_HADOOKEN);
			break;

		}

		_currentAnimationState = state;
	}

	//--------------------------------------
	// Check if player has collided with the floor
	//--------------------------------------
	 void OnCollisionEnter2D(Collision2D coll)
	 {
		 if (coll.gameObject.name == "Floor")
		 {
			 _isGrounded = true;
			 changeState(STATE_IDLE);

		 }

	 }

	 //--------------------------------------
	 // Flip player sprite for left/right walking
	 //--------------------------------------
	 void changeDirection(string direction)
	 {

		 if (_currentDirection != direction)
		 {
			 if (direction == "right")
			 {
			 transform.Rotate (0, 180, 0);
			 _currentDirection = "right";
			 }
			 else if (direction == "left")
			 {
			 transform.Rotate (0, -180, 0);
			 _currentDirection = "left";
			 }
		 }

	 }

}

If you run the game you may find the player is falling through the floor. This is because Unity’s 2D physics engine was leveraged to create the simple jump movement.

To to allow the player to walk on a floor we will need to do 2 more things:

Adding a floor

Create and add a floor. Select GameObject > Create Empty.

Name it ‘Floor’.

With the floor selected, click ‘Add Component’ in the Inspector window then Physics2D  > Box Collider 2D. This will add a box collider to the floor so the player has something to stand on. In the Inspector change the Box Collider’s Size properties so it fits the screen, then move the whole Floor object to a suitable position below the players feet.

UnityStates3_5

 

Adding Collider and RigidBody2D to the Player

Select the player and as in the previous step, add a 2D Box collider, only this time to the player. You can either position it at the players feet or make it the size of the while player, either will work in this case.

Now we need to add a rigidBody2d to the Player. With the Player selected, click ‘Add Component’ in the inspector window, then Physics2D  > Rigid Body 2D. Leave the settings as default.

If all goes well running the game should give you some positive results. But as is the way with development you are likely to have some errors!

Incase you run into such problems which you cannot solve, please treat yourself to the Project Source Files on my git repo. Feel free to leave a comment if you have any questions.

Keyboard controls are: Up =  Jump,   Left/Right = Walk,   Down =  Crouch,   Space Bar =  Hadoooooooken

 

Ken movement

 

 

Join me on Patreon to get exclusive access to source code and valuable gamedev info.

Become a Patron!