Help please I need a jump feature

ok, thanks
i noticed it too

var myscript = pc.createScript('myscript');


myscript.prototype.initialize = function(){
    this.canJump = false;
    this.entity.collision.on('collisionstart',function(){
    this.canJump = true;
    },this);
};

myscript.prototype.update = function(dt){
   if(this.app.keyboard.isPressed(32) && this.canJump === true)){
        this.entity.applyImpulse(0,40,0);
        this.canJump = false;
   }
};


1 Like

Hi @Fus_ion! Your approach may be better, but your code is not correct. You still have to check this one.

1 Like

ok im going to be honest i don’t know how to code but I need help

Hi @wolfshadow726! What do you need help with? What is your result so far with the help above?

1 Like

well, my main problem is that I don’t know where to enter the code at.

That depends on what the rest of your script looks like. If you add me to your project I can take a look.

I know some might have issues with this code, maybe cause it’s outdated, or as @Albertos stated, the code was wrong.

I made my own version of this script that works perfectly fine and doesn’t rely on height (which can be unhealthy for people who have maps with buildings and stuff)

Here it is:

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

Jump.prototype.initialize = function(){
    this.canJump = false;
    this.entity.collision.on('collisionstart',function(){
        this.canJump = true;
    },this);
};

Jump.prototype.update = function(dt){
   if (this.app.keyboard.isPressed(32) && this.canJump === true) {
        this.entity.rigidbody.applyImpulse(0, 1000 ,0);
        this.canJump = false;
   }
};
1 Like

This seems like it would make it possible to jump again as soon as you collide with anything having it’s own rigidbody/collision component (like colliding with a wall would make it possible to jump again, I think, depends on the size of your collision box).

What I would do is bind the canJump to the entity that can jump (this.entity.canJump…) and have a semi-small collision box on the bottom of the player feet that checks for collisions. When it collides with anything it will allow the player to jump again (or I can specify the collision with an object with a tag).

1 Like

I don’t think a part under the player is efficient, is there a way to detect where the player is being touched? (From the top of the entity, bottom, etc.)

It really depends on how big the project is. Sometimes just checking if the z distance is less than 0.1 is enough.

For some of the projects that I work for/worked for, we had to put a collision box on each part of the player body and assign a tag to it so that we can check if the collision is on the forearm, legs, chest, etc.

  • You could launch a raycast from the bottom of your character and check the distance.
  • You could launch 4 raycasts (corners) and check if any is near the floor.
  • Place a small colider under your character (it works quite well)
  • Check the velocity of your character and if it is ~0 you are on the ground.
  • Launch a parabole from your characters start jump point and angle and calculate manually how long the jump should last.

There are more but I am really tired and just came back from a party. Hope it helpes :slightly_smiling_face:

Hey, just now checked, regardless of if you have a wall, and you are colliding with it, you have to be on the ground to jump again.

Never mind, you were right.