Enhance jumping feature


When holding both the space key to jump and the W key to move forward, the object jumps diagonally, but when releasing both keys, it suddenly falls straight down. I want to fix it so that there is a remaining horizontal velocity that causes the object to fall diagonally, in line with the correct physical context.
Here is my project: Player | Editor (playcanvas.com)

Hi @end_tr_n and welcome!

As far I can see you use teleport() to move the entity forward.

So basically you only change the position of the enitity without applying a force using applyForce() or applyImpulse(), like you do for jumping.

1 Like

I think the most easy solution is to stop normal movement if the player is jumping and apply a force forward and upwards.

1 Like

ahh i saw it, could you pls help me fix this code to apply a force

I’m not sure what the exact way should be, but you can try something like below first.

In this code I only execute teleport() when the player is grounded, so it hopefully doesn’t break the jump force, and I also applied a forward jump force (assuming forward is the negative z-axis). If it works this way you can adjust the value of the forward force if necessary.

    // Apply movement force
if (this.isGrounded) {
    if (this.force.x !== 0 || this.force.z !== 0) {
        var pos = new pc.Vec3(this.force.x * dt, 0, this.force.z * dt);
        pos.normalize().scale(this.speed);
        pos.add(this.entity.getPosition());

        var targetY = this.cameraScript.eulers.x + 180;
        var rot = new pc.Vec3(0, targetY, 0);

        this.entity.rigidbody.teleport(pos, rot);
    }

    // Jump logic
    if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
        this.entity.rigidbody.applyImpulse(0, this.jumpPower, -this.jumpPower);
        this.isGrounded = false;
    }
}
1 Like

i have tried, but when i press space button, the screen when to dark

I did a quick test and if you get a black screen you try to apply a force that doesn’t exist. I see you renamed this.jumpPower to this.jumpPower1 for example, so that can cause the problem.

When you use the code below, you can get the result of the video. Make sure this.jumpPower exist and has the correct value. I changed the forward jump force with this.force.z that is also used for normal movement.

    // Apply movement force
  if (this.isGrounded) {
      if (this.force.x !== 0 || this.force.z !== 0) {
          var pos = new pc.Vec3(this.force.x * dt, 0, this.force.z * dt);
          pos.normalize().scale(this.speed);
          pos.add(this.entity.getPosition());

          var targetY = this.cameraScript.eulers.x + 180;
          var rot = new pc.Vec3(0, targetY, 0);

          this.entity.rigidbody.teleport(pos, rot);
      }

      // Jump logic
      if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
          this.entity.rigidbody.applyImpulse(0, this.jumpPower, this.force.z);
          this.isGrounded = false;
      }
  }

1 Like

could you give me this link project, i tried to use your code again and it still can not be fixed, still falls straight down when i releasing both keys

You’re right, I’ll look into it again later today.

1 Like

Hi @end_tr_n!

I think the First Person Shooter starter kit has a jump function that works the way you want.

1 Like

i have tried many time but it’s not successfull, i think the different between third person and first person make it harder further more i’m new to playcanvas engine so i’m not understand everything well. Thank you for all, if you have freetime, could you help me to apply this code to my project

You can do something like below.

// Apply upwards jump force
this.entity.rigidbody.applyImpulse(this.entity.up.clone().scale(this.jumpPower));

// Apply forwards jump force
this.entity.rigidbody.applyImpulse(this.entity.forward.clone().scale(this.jumpPower/2));