How do I implement a jump mechanic? HELP


#1

So I am trying to implement a jump mechanic in my code.I’m trying to create a first person game but to be honest I suck at coding. I managed to find and figure out how the first person controls work but I have a problem with implementing the jump mechanic

I did the following:

if (app.keyboard.isPressed(pc.KEY_SPACE)){

           this.entity.rigidbody.applyImpulse(0,2,0);

          }

but if I keep mashing space I just fly off. IDK how to make sure I can only jump while in contact with another surface or at least how to time out the jump button
NOTE: my floor is constanly moving upwards so I presume an “on colission” trigger would be my best choice

EDIT:
I added a non collision trigger but it’s still not working

˙˙˙
this.entity.collision.on(‘jumpstart’, this.onJumpStart, this);

onJumpStart = function (result) {

if (result.other.rigidbody) {
    if(result.other.name === "Fallblock"){
     if (app.keyboard.isPressed(pc.KEY_SPACE)){

       this.entity.rigidbody.applyImpulse(0,2,0);
        }
      }
}

};
˙˙˙

FINAL EDIT:
I figured it out thanks to this guys project
https://playcanvas.com/project/569492/overview/tutorial-third-person-camera


#2

Make sure ur gravity is turned up and ur Dynamic body is 100


#3

Thats good now link the rigidbody only able to jump when it is in contact with the ground


#4

I tried doing that in several ways but my floor is moving upwards since I’m trying to build a kind of a 3d platform arcade game… I tried comparing the Y positions but it doesn’t seem to work … I presume I’d need an on colission trigger to make it work, but IDK exactly how to make one


#5

This project is very useful, but the jump is not ideal. A double jump is possible, and if you are standing on the edge of a cliff, for example, then the raycast will not detect the surface and the jump will not work. In my project, I used a combination of a raycast check and a collision contact.


#6

thanks for the suggestion, I may look into it