- Home /
2D Top Down Character Controller
I am trying to make a top down 2D game, sort of in the style of classic Zelda, at least the movement. Thats all i need to start moving on and ive been stuck looking for help on this for days now. I know its so simple, but i cant seem to figure it out. This is what i have, i just copied the movement up and pasted it 3 times because i figured it would be same just different vector. But I dont know what i need to put in its place for the other directions. Please help!!
pragma strict
//Inspector Variables
var playerSpeed : float = 10; //speed player moves
function Update ()
{
MoveForward(); // Player Movement
}
function MoveForward()
{
if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0,playerSpeed * Time.deltaTime,0);
}
if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0,playerSpeed * Time.deltaTime,0);
}
if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0,playerSpeed * Time.deltaTime,0);
}
if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0,playerSpeed * Time.deltaTime,0);
}
}
Answer by Chris333 · Jan 23, 2015 at 08:11 PM
Hi,
that should do it. You needed just to change the values in the Translate() method to the specific direction. http://docs.unity3d.com/ScriptReference/Transform.Translate.html
#pragma strict
//Inspector Variables
var playerSpeed : float = 10; //speed player moves
function Update ()
{
MoveForward(); // Player Movement
}
function MoveForward()
{
if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0,playerSpeed * Time.deltaTime,0);
}
if(Input.GetKey("down"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(0,-playerSpeed * Time.deltaTime,0);
}
if(Input.GetKey("left"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(-playerSpeed * Time.deltaTime,0 ,0);
}
if(Input.GetKey("right"))//Press up arrow key to move forward on the Y AXIS
{
transform.Translate(playerSpeed * Time.deltaTime,0 ,0);
}
}
A good start how to make 2D games is the following tutorial. You can easily change the script in the video to make a top down character moving. http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
Okay! Thanks a bunch! Now I just need to figure out how to do collisions. I have added box colliders to player and objects but you can still go right through. Could you help me with that as well possibly?