Skip to content

How to detect if an actor is on the ground? #2406

Answered by eonarheim
tenpaMk2 asked this question in Q&A
Discussion options

You must be logged in to vote

@tenpaMk2 This is a good question!

There are a couple tricks that can be used to detect on ground in a situation like this:

  1. You could use your most of your existing code and keep track of all sides collided in a frame, if Side.Bottom is in the list of sides that collided that frame you are on the ground.
export class GameScene extends Scene {
  playerOnGround = false;
  playerSideCollisions: Side[] = [];
  onPreUpdate() {
    playerSideCollisions.length = 0; // clear last frame's side collisions
  }
  ...

  isPlayerOnGround() {
    return playerSideCollisions.includes(Side.Bottom);
  }

  onInitialize() { 
    ...
    player.on("postcollision", (event: PostCollisionEvent): void => {

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@tenpaMk2
Comment options

Answer selected by tenpaMk2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants