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

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

it isnt working for me

HI All,

spitfireyt just published this example to show FPS. Included is a very clean method to handle the jump as I was pointing out above.

https://playcanvas.com/project/966421/overview/basic-first-person-template

2 Likes

ty, sorry for late reply, i was on vacation

you need to rename the ‘jump’ to Jump because thats the name of your script

how to make player jump only one time

Below an example project.

https://playcanvas.com/project/1063800/overview/basic-jumping

Same problem here, @Leonidas can you help us?
/when I press my ‘Jump button’, it teleports me back to my start

This script you gave lets you infinitely jump without touching the ground to jump again. Could you please give the code for a traditional jump?