[SOLVED] How To Make Script That Allows Player To Jump?

You can select an entity and use the inspector panel in the editor to add a script component.

From there you can click and find your jump.js script and add it to your entity.

https://developer.playcanvas.com/en/user-manual/designer/inspector/

could you send a screenshot of what im looking for

  1. Select an entity and to the right click Add Component and add a script component.

  1. Then click on the empty input field that appeared together with the script component and find your jump script:

image

1 Like

i added it and it still not working

Try increasing the impulse value to something bigger, like 100 or 1000, the body has a big mass.

If that doesn’t work, try posting a sample project to take a look why it doesn’t work for you.

https://playcanvas.com/project/770644/overview/my-project-copy
That was a copy of what I did. I did everything that u told the other person to do but it’s not working for me. Please help as this is the first game that I am creating. Thanks!

1 Like

Hey @Gameinator, I just had a quick look at your project, the jumping script is empty?

1 Like

That’s weird maybe try again right now. Any time I click the file it shows the code?

Have you saved the script? (CTRL+S/CMD+S)

It should work now I think I forgot to copy the code or something

Hey @Gameinator, you had missed a few braces while copying the code, and the force value was too low due to the mass of your character. I’ve forked your project and fixed both - https://playcanvas.com/editor/scene/1102837. My suggestion would be to initially write the code by hand, and not simply copy paste in the beginning, since that’ll help you learn the PlayCanvas scripting system a bit better. Do go through the following tutorials, they’ll help you learn the engine -

  1. Daniel Wood’s Getting Started With PlayCanvas
  2. The official “Keepy Up” Tutorial"

Good luck with your game!

1 Like

I was trying to code in my game and I noticed that the “bean” was double jumping is there a way to fix that

Hi @Gameinator, all you’ll need to do for that is limit the Y-axis movement like so -

if(this.app.keyboard.wasPressed(pc.KEY_SPACE) === true){
    if (this.entity.getLocalPosition().y < //insert correct max y limit here) {
         this.entity.rigidbody.applyImpulse(0, 5, 0);
    }
} 

Essentially, what we’re doing here is limiting how high the capsule can jump, and therefore stopping the double jump from happening.

how does this work? ive tried putting the y level in the () things, replacing the y with my chosen y level, tried putting it as y.1500 (my chosen y level) tried putting it before the < thing but nothing works it always has some issue

1 Like

Hi All,

Here are the settings I have used in my first person game. First the code I put into the fps-controller script.

if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
    
    console.log("Got to Jump");
    this.entity.rigidbody.applyImpulse(0,1000,0);
}

Settings for rigid body
image

and don’t forget to make this change to gravity in

Make sure you import Ammo

ty ima try this

i like how it feels, but i still have a infinite jumping thing, do you know how to fix it?

Can you give me a little more description? So is it that you hit the spacebar once and it keeps jumping? Do you have a public project you would be able to share?

no the issue is if you hit space while in air i can jump infinitly, also i do, ill send link
https://playcanvas.com/editor/scene/1480292

Hi Brandev,

I was just writing the code to show you that you will have to detect a collision when you are on the ground and stumbled upon the following.

This method is the best for stopping the infinity jump. So what I have done is exactly what is described in the code snippet. Give your ground or solid objects like steps, ramps, etc. a tag of ‘ground’.

image

So in summary when the player has the collision you read the tag and set a flag to indicate if the player is on the ground or not

1 Like