CHANNELS:

All
M
Molymongo posted 12 days ago
chris posted 6 days ago

You can add a basic condition that checks if your player is currently moving on the y axis, like:

if (player.velocity.y == 0) player.velocity.y = -10;

You can extend this further by adding a falling state, as to say, if by the odd chance your character's y velocity is 0 when they've reached the peak of the jump, you don't want to be able to reset the player's y velocity back to -10 (this would cause more jump spam).

So what you could do is within your event listener:

if (player.velocity.y !== 0 && !player.travelingUpwards) {
  player.velocity.y = -10
  player.travelingUpwards = true
}

Then for every frame within your animation loop, check if the player is falling down:

if (player.velocity.y > 0) {
  player.travelingUpwards = false
}

Combined together, you should prevent jump spam and prevent a player from jumping if they reach the peak and their y velocity is 0.

0

Want to participate?

Create a free Chris Courses account to begin

or
Sign In

Providing the lift to launch your development career

© 2023 Chris Courses. All rights reserved.