2D player controller

ok so I went through the whole tornado twins worminator tutorial and got a really good grip on the basics of scripting in unity. now I figured I’d give myself a try at a basic 2D platforming game (kinda like the old mario games) and then maybe try to add some neat features to it once I get a bit better.

currently I’m stuck in the first part which is character movement. I know I could easily enough take the script from the 2D tutorial here on Unity3D’s website but then I wouldn’t learn anything, so I was hoping you guys could give me some advice on this script I have here.

var runSpeed = 10.0;

function Update(){

	//Movement buttons are pressed
	if(Input.GetButtonDown("Horizontal")){
	
		//Set some variables for movement
		var moveSpeed : float = Input.GetAxis("Horizontal")*runSpeed;

		//Move our character
		transform.Translate(moveSpeed, 0, 0);
	
	}

}

it works, except it doesn’t continously move me. I’ll be holding down the “D” key, for example, and I will only move once and I have to press it again to continue to move. idk if I should have the movement in its own function (it seems like in the Update would be the best place for it) or if I’m just not calculating the moveSpeed variable right.

I’ve tried messing around with TransformDirection() too but also can’t seem to figure it out.

thx in advance for any help.

Hi

Use Input.GetButton (“Horizontal”) in the first if

if you use GetButtonDown, you only get the signal one time when you push down the button.
else if you use GetButton, you get the signal always while is pressed the button.

The problem is in the GetButtonDown. Unity offers three different input states you can monitor:

  • GetButton – Returns true every frame the button is being held down
  • GetButtonDown – Returns true on the frame where the button is pushed. Note: Only on that single frame.
  • GetButtonUp - Returns true on the frame the button is no longer pressed. Same as above, only on a single frame.

So you just have to replace the GetButtonDown with GetButton. Though Input.GetAxis returns 0 if the left/right buttons are not pressed, so the GetButton if-clause is a bit redundant (might save a fraction of a millisecond though).

thx guys :slight_smile: works now. I figured it’d be something simple lol.

you should make moveSpeed a vector3, so you could controll the y axis and the z axis if you want.
and you shouldnt check if you are pressing a button before changing moveSpeed’s value.
by doing something like this:

var moveDir = Vector3.zero;
var gravity = 9.0;
var jump = 6;
var controller;

function Awake()
{
    controller = GetComponent(CharacterController);
}

function Update()
{
    moveDir.x = Input.GetAxis("Horizontal") * speed;
    moveDir.y -= gravity*Time.deltaTime;
    if(Input.GetKeyDown("space"))
        moveDir.y = jump;
    controller.Move(moveDir * Time.deltaTime);
}

why are you using Translate to move a character?
high chances that your character will continue walking if you are colliding with a wall.
you should use a Character Controller with the function Move().
the code is simple, you just determine the direction of the player in the 4 first lines of the update function
and than you move the player with the function Move() by that direction.
good luck.

well I was trying to use Translate because I’m not that good with scripting yet so I couldn’t figure out Move (well I was using SimpleMove), lol.

that script seems to be exactly what I was looking for (went through a bunch of trouble for the jump function, you shortened it by about 20 lines and made it 100x smoother =/ lol) so thanks for that.