How to make a jump using physics

i am newish to playcanvas and I do not know the first thing about coding in JS for games, my movement script is a copy and paste. if you dont mind, please put the code needed in the comments and how to make physics because physics make me go yes

Hi @gamerlol5 and welcome! If you already have a script you can add the code below or use this as example.

In your initialize function:, this.onKeyDown, this);

In your update function:

var force = 500;

if ( {
    this.entity.rigidbody.applyImpulse(0, force, 0);

thank you

New to the engine, got a question about this code piece: where was this.onKeydown implemented? Isn’t the keyboard related stuff handlings in ~.prototype.update unrelated with the previously attached event listener?
BTW I suppose the latter piece of code would not work properly if the space bar is kept pressed. Maybe when you said update you meant this.onKeydown?

You are right, the event listener is not needed in this example.

You mean the jump function is not executed when the space bar is not released?

Nah, i mean if the user keeps pressing the space bar, then the impulse would be continuously applied to the entity, causing it to escalate quickly.

That’s correct, but I’m not creating the game, I just show how you can create a basic jump action that you can improve yourself. All extra statements to prevent for example infinity jumping are based on the setup of the project. If you want, feel free to share a better solution.

I’ve posted my solution here. :+1:

1 Like