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.)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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.

1 Person Made This Project!

Recommendations

  • Art Skills Challenge

    Art Skills Challenge
  • Make it Move

    Make it Move
  • Teacher Contest

    Teacher Contest

7 Discussions

0
None
Nanite

3 years ago

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.

3 replies
0
None
christoph2005Nanite

Reply 2 months ago

Also the website is editing the code that gets pasted here.
It removed my <> values alltogether

0
None
christoph2005Nanite

Reply 2 months ago

Very true.
I believe it's meant to be incomplete for you to complete it. That way you have to think about it, instead of just copy paste.

I didn't hook up my animations but I noticed the "GetComponent" bug you mentioned. If you're familiar with Unity scripting a little bit, you'll know what you need to do to fix those lines; like Luther pointed out.

Additionally, the "order" in which the update evaluates the input causes very dramatic differences when jumping left vs jumping right. For me: I couldn't jump at all when moving left, however when moving right it would jump to the stars and fly off the screen.

After fixing some of the import bugs, and changing the order in which the inputs are evaluated in the Update function it seems like a very workable controller, also very simple.

Here's my version of the code from this tutorial:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
//Animator anim;
// public float smooth = 1f; // <- this is weird tho, it isn't used anywhere ...
public float speed = 6.0F;
public float jumpSpeed = 0.0F;
public float gravity = 20.0F;
private Quaternion lookLeft;
private Quaternion lookRight;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
public float getMoveSpeedX(){
return moveDirection.x;
}

void Awake(){
controller = gameObject.GetComponent();
//anim = GetComponent ();
}

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

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

void Update() {
if (controller.isGrounded) {

//anim.SetBool ("IsRunning", false);

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


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

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

//anim.SetBool ("IsRunning", true);

}

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

```
0
None
LutherL1Nanite

Reply 2 years ago

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

0
None
spasaev226

1 year ago

can you give me Vincent 3D model because mixamo store closed