[SOLVED] Need help with a jump code

heres my code can someone please help

var Jump = pc.createScript('jump');

// initialize code called once per entity
Jump.prototype.initialize = function() {
        
    if (this.app.keyboard.isPressed(pc.KEY_SPACE)){
        this.entity.translateLocal(0,1,0);
    }
    
};

// update code called every frame
Jump.prototype.update = function(dt) {
    
};

Hi @the_amazing_ella! You have to replace the code from your initialize function to your update function.

Ha I made a made a big yet small mistake, thank you for pointing that out

1 Like

now i need to simulate gravity because i float when i jump

If you use a dynamic rigidbody you have to move your rigidbody instead of your entity.

if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
    this.entity.rigidbody.applyImpulse(0, 500, 0);
}

thank you

i can fly hahahahahhahah

I think that’s a cool feature for your game. Otherwise you have to add extra statements to prevent you can jump in the air. An easy way is to use for example a timer. For a better solution you can search on the forum, because there are a couple of topics about that.

yeah i dont know how to do that but i kinda need to jump not really fly sooo.

Something like below. (You can adjust the values).

this.timer += dt;

if (this.timer > 2 && this.app.keyboard.isPressed(pc.KEY_SPACE)) {
    this.timer = 0;
    this.entity.rigidbody.applyImpulse(0, 500, 0);
}

is this it

var Jump = pc.createScript('jump');

// initialize code called once per entity
Jump.prototype.initialize = function() {
    
};

// update code called every frame
Jump.prototype.update = function(dt) {
    
    
    this.timer += dt;

if (this.timer > 2 && this.app.keyboard.isPressed(pc.KEY_SPACE)) {
    this.timer = 0;
    this.entity.rigidbody.applyImpulse(0, 500, 0);
}
    
    if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
    this.entity.rigidbody.applyImpulse(0, 100, 0);
}
    
};

// swap method called for script hot-reloading
// inherit your script state here
// Jump.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

You have to remove the second statement and then check if it works.

what statement i don’t know what that a statement is hehehehehehe

The line of code that starts with if or else. Right now you check two times if the user press the space bar. You only need the first one with the timer.

is this right

var Jump = pc.createScript('jump');

// initialize code called once per entity
Jump.prototype.initialize = function() {
    
};

// update code called every frame
Jump.prototype.update = function(dt) {
    
    
    this.timer += dt;

 (this.timer > 2 && this.app.keyboard.isPressed(pc.KEY_SPACE)); {
    this.timer = 0;
    this.entity.rigidbody.applyImpulse(0, 500, 0);
}
    
    if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
    this.entity.rigidbody.applyImpulse(0, 100, 0);
}
    
};

// swap method called for script hot-reloading
// inherit your script state here
// Jump.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

No, you only need the code in your update function that I sent you. You need to learn some basics first to be able to make a game.

ohhhhhh ok

i know some very basic stuff like console.log()

um it didint work

var Jump = pc.createScript('jump');

// initialize code called once per entity
Jump.prototype.initialize = function() {
    
};

// update code called every frame
Jump.prototype.update = function(dt) {
    this.timer += dt;

    if (this.timer > 2 && this.app.keyboard.isPressed(pc.KEY_SPACE)); {
        this.timer = 0;
        this.entity.rigidbody.applyImpulse(0, 500, 0);
    }
};