user3533784
user3533784

Reputation: 53

No Script Errors - Character doesnt jump - Unity 3d

Currently building a 2D game, and have created a javascript code for the jump for my character 'Ezio' but it does not do anything when i press 'space' for it to jump. There are no errors with the code either.

#pragma strict
var jump :float = 0; 
var jumpspeed : float = 15; 
var jumptimer :float = 0; 
function Start () {
}
function Update () {
    if (jump == 1) {
        jumptimer = jumptimer +1;
        if (jumptimer >= 50) {
            jumptimer = 0;
            jump = 0;
        }
    }
}
if (Input.GetKeyDown ("space"))
{
    if (jump == 0) {
        rigidbody2D.velocity.y = jumpspeed;
        jump = 1;
    }
}

Any suggestions on what could be the issue?

Upvotes: 1

Views: 721

Answers (1)

display-name-is-missing
display-name-is-missing

Reputation: 4399

Try this:

#pragma strict
var jump :float = 0; 
var jumpspeed : float = 15; 
var jumptimer :float = 0; 
function Start () {
}


function Update() {

    if (Input.GetKeyDown("space")) {

        if (jump == 1) {
            jumptimer = jumptimer + 1;
            if (jumptimer >= 50) {
                jumptimer = 0;
                jump = 0;
            }
        } else {
            rigidbody2D.velocity.y = jumpspeed;
            jump = 1;
        }
    }

}

Upvotes: 2

Related Questions