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

About: Lecturer, Interactive Design and Game Development, Faculty of Information Technology, Dhurakij Pundit University http://www.dpu.ac.th/it

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: https://www.mixamo.com/store/#/ 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 mixamo.com.Sign 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: https://www.assetstore.unity3d.com/en/#!/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>

to

<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 = Vector3.zero;

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.