Introduction: Unity 2.5D Character Control in Platform Side-scrolling Game

This tutorial demonstrates how to use the visual editor of Unity program, and basic guideline scripting with C# language on MonoDevelop, animator basics, the basics of movement character contorl and rotating to flip direction of game object. (about 2.5D game, it’s basically a 3D (X,Y,Z) game that you squish so the gameplay is only along a 2D (X,Y) axis.)

Step 1: Open Up Unity and Create New Project (3D)

When you launch unity program,insert the game name for your project name, location to store project some where in you computer and select the 3D option.

Step 2: Insert the Plane and Setting the Inspector

Go to the 'GameObject' in the tool bar, find for 3D Object > Plane.

Look at the inspector tab of GameObject (Plane) change thescale and position values. In this tutorial use sample position in X axis, Y axis and Z axis to 0.

Step 3: Create 3D Model

You will need to download the 3D Model for Player Character. Find it in the website: Looking for the "Free Test Pack" (in this tutorial choose Vincent).

Step 4: Download 3D Model and Import to Unity

when you select the 3d model from Up Create Account, login and click "Add to cart".

Click Download and Checkout choose export type to "FBX for Unity" and Download it. Open the zip file in your computer and Drag and Drop model's folder in the Projects window > Assets to importing the model in to the project.

Step 5: Make Humanoid for Animator Controller

Select the 3d model in Scene window, look at the Inspector click "Select" tab.

When the option in inspector change select "Rig" tab choose "Humanoid" types and "Apply". when it ok, click configuration button.

Step 6: Mapping and Make Muscles & Change Camera Position

Click all green circle point to mapping all bone connection in 3D model. Check muscles work in the muscles tab.

Change camera's position in X axis to 14, Y axis to 270 and Z axis to 0.

Preview your game in Game View for checking camera position side scrolling.

Step 7: Create Animator Controller

I use animation from Asset Store "Mecanim Example Scene" (Idle, Run) Check out:!/content/5328

Create Animator Controller from Right click in Project Asset Tab. Put the "Idle" State to default and make transition to "Run" state with create parameter "bool" named it "IsRunning".

Step 8: Test Animation From Animator Controller

Now in your scene, go to the Player model and drag the Animator Controller on to the animator object.
Un check "Apply Root Motion" and The culling mode set it to "Always Animate".

Step 9: Control the Character

Add a Character Controller and Capsule Collider component to Player by this 3 steps:

  1. Add Component > Physics > Capsule Collider (check "Is Trigger" option).
  2. Add Component > Physics > Character Controller.
  3. Add Component > Physics > Rigidbody (uncheck "Gravity" option).

Step 10: Make the 3D Game Support in Only a 2D Axis.

Create the C# file named it "Player.cs" and make Movement character with X,Y axis only, change standard code from:

<p>moveDirection = new Vector3(-(Input.GetAxis("Horizontal")), 0, Input.GetAxis("Vertical"));</p>


<p>moveDirection = new Vector3(-(Input.GetAxis("Vertical")), 0, Input.GetAxis("Horizontal"));</p>

The code looking this:

<p>void Update() {<br>  CharacterController controller = GetComponent<charactercontroller>();
   if (controller.isGrounded) {
       moveDirection = new Vector3(-(Input.GetAxis("Vertical")), 0, Input.GetAxis("Horizontal"));</charactercontroller></p><p>         if (Input.GetButton("Jump"))
	     moveDirection.y = jumpSpeed;</p><p>    }</p><p>}</p>

Step 11: Code It!

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour { Animator anim; public float smooth = 1f; public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Quaternion lookLeft; private Quaternion lookRight; private Vector3 moveDirection =;

void Start(){ Cursor.visible = false; anim = GetComponent (); Time.timeScale = 1;

lookRight = transform.rotation; lookLeft = lookRight * Quaternion.Euler(0, 180, 0); }

void Update() { CharacterController controller = GetComponent(); if (controller.isGrounded) {

anim.SetBool ("IsRunning", false);

moveDirection = new Vector3(-(Input.GetAxis("Vertical")), 0, Input.GetAxis("Horizontal"));

if (Input.GetButton("Jump")) moveDirection.y = jumpSpeed;

if (Input.GetKey(KeyCode.A)){

transform.rotation = lookLeft; moveDirection = transform.TransformDirection(-moveDirection); moveDirection *= speed;

anim.SetBool ("IsRunning", true);


if (Input.GetKey(KeyCode.D)){ transform.rotation = lookRight; moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; anim.SetBool ("IsRunning", true); } } moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } }

Step 12: Let's Play and Have Fun

Let's Play your Game by key A,D button on your keyboard, and see the rotation of transform direction of the Game Object.


dr_hazrd made it! (author)2017-08-11

I there a reason that when you press both 'w' and 'd' buttons while constraining rotations and position on the z axis, you move along the z axis.

Nanite (author)2016-03-02

It's strange that even though this instructable is over 8 months old, no one has pointed out that your code doesn't work. Your two GetComponent calls do not call anything. The second GetComponent is supposed to be GetComponent<charactercontroller>() but I can't figure out what the first GetComponent is supposed to be getting. I think the problem is the website may be stripping out whatever is between the <> characters. The code as-is just generates compiler errors in Unity 5 and doesn't do anything. It's a shame because the rest of the instructions are pretty solid and if the code worked it would be a really great primer on 2.5D in Unity.

LutherL1 (author)Nanite2016-10-29

The first one is GetComponent<Animator>(); This is to change the animation when the direction button is pressed.

seamster (author)2015-07-08

Nicely done. Thanks for sharing this!

daydev (author)seamster2015-07-08

@seamster, Thanks you very much.

About This Instructable




Bio: Lecturer, Interactive Design and Game Development, Faculty of Information Technology, Dhurakij Pundit University
More by daydev:Unity 2.5D Character Control in Platform Side-scrolling Game
Add instructable to: