Raycast to check for bottom of block?

So I’m trying to figure out how to cast a simple raycast from the top of my bounding box & see if my character hit head on bottom of block.

how to do this?

Picture?

Is it not a simple overlap check?

Yes, but I need to specifically check for each side of the box. Because I don’t want it activating when I’m standing on top of it, rather I want it to activate when I hit it with character’s head.

Check (char.body.touching.up && box.body.touching.down) in a collision callback.

How would I do a raycast so I can check if my player is about to fall down from a jump or a ledge?

someone can help me?

@samme : how?

this.physics.add.collider(
  char,
  block,
  function (_char, _block) {
    if (_char.body.touching.up && _block.body.touching.down) {
      // …
    }
  });

@samme : the above causes an error with touching.down. Cannot read property 'touching' of undefined after i attempt to jump… also, how would I modify this to check for falling off the edge of a block or a ledge or edge?

@samme : how would i do this?

You have to look at the _block value (or whatever it’s called). It should have a body property.

If you want to check if the character just started falling:

// In update()
if (char.body.touching.none &&
  char.body.wasTouching.down &&
  char.body.velocity.y > 0) {
  // Character started falling??
}

If you want to look at the character’s position on the platform:

this.physics.add.collider(
  char,
  block,
  function (_char, _block) {
    if (_char.body.touching.up && _block.body.touching.down) {
      // Character hit block from below.
    } else if (_char.body.touching.down && _block.body.touching.up) {
      // Character is atop block.
      if (_char.body.center.x < _block.body.left) {
        // Character is nearly past left edge of block.
      } else if (_char.body.center.x > _block.body.right) {
        // Character is nearly past right edge of block.
      }
    }
  });

@samme : this doesn’t work. :frowning:

this.physics.add.collider
(
	this.player.sprite, this.__yellowBlock.sprite, 
	function ( __char, __block ) {

		console.log ( 'touching up :: ' + __char.body.touching.up );
		if ( __char.body.touching.up && __block.body.touching.down )
		{
			// Character hit block from below
			alert ( 'BOTTOM!' );
		}
		else if ( __char.body.touching.down && __block.body.touching.up )
		{
			// Character is atop block
			if ( __char.body.center.x < __block.body.left )
			{
				// Character is nearly past left edge of block
				alert ( 'LEFT!' );
			}
			else if ( __char.body.center.x > __block.body.right )
			{
				// Character is nearly past right edge of block
				alert ( 'RIGHT!' );
			}
		}
	}
);

i will add : i am colliding with a TILE layer called block. not an object. if i must change it to an object, i shall.

I want to clarify the question. You want to know will your character hit the box’s top border before it will happen?

@RollinSafary : No. I want to detect when the character has just fallen off the block & is in the air & at that IMMEDIATE second, switch to the falling animation. Sometimes when my character is on the VERY edge like 1-pixel thin of falling off the edge, my character twitches between idle & falling which I don’t want. I also want to hit the bottom of the block with my head & make the box act like in mario where it activates something. I hope I explained well enough! :slight_smile: If not, please do let me know & I will do my best to clarify even further! Thanks!

can you provide information about your character’s origin settings ?

Remove all the __block.body expressions.

__block will be a Tile. You can use methods like getBottom() etc. if you need.

so wait. do :

__block.touching.down

instead of :

__block.body.touching.down

?

I think that you have some check to know that character is overlapping with the box. There might be additional check to decide does character falling down or not. In idea the falling down can be detected by checking normal distance of their y coordinates and current. So

this.normalDifY = this.character.y - this.box.y; // When character have just stand on the box
// this can be calculated another way to have in fact true values even for cases when player will fall after standing on the box

then I guess you’re going to detect falling moment in update function.
So you can just do the following

// ... if overlaps 
this.currentDifY = this.character.y - this.box.y; 
if(this.normalDifY - this.currentDifY > 0){
 this.character.play('fallAnimation');
}

@RollinSafary :

// this can be calculated another way to have in fact true values even for cases when player will fall after standing on the box

how?