Jump to content

Find out if there is an object above in a specific distance to player


valueerror
 Share

Recommended Posts

i need to check if there is an object above the player with which i doesn't collide yet (but will collide if he jumps)..  so lets say: i want to find out if there is something 40 pixels above my player..

 

or in other words:  ;-)

 

if (isObjectOnPosition(player.y-40)){ wouldcollideifjump = true; }

 

thx

Link to comment
Share on other sites

The best way to accomplish this would be to have an invisible sprite with a body positioned 40 pixels above the player and check that sprite for overlap whenever the player presses the jump button, thus allowing you to determine what's above the player (if anything).

Link to comment
Share on other sites

I don't think raycasting works this way in Phaser; only TilemapLayers implement this natively. Raycasting is indeed one of the ways you'd do it in Unity for instance, but not yet in Phaser, without doing a lot of line intersect stuff a la this example: http://gamemechanicexplorer.com/#raycasting-1

 

I should elaborate that with my method, your detection body should be 40px tall and positioned so the body.bottom is 1 pixel above the player.body.top so you don't end up with gaps and detect everything potentially above the player.

Link to comment
Share on other sites

if i would use tilemaps i could use the gettileworldXY function to find out if there is a tile on the given xy position on a specific layer... Thats the way i did it until now.. But today i changed all collision layers to object layers and i convert the polylines i paint into physics bodies so all getTile methods are out of service now...

maybe i will add a sensor shape to my player and check for collusions on this shape.. I just hoped there is a way to obly check for this potential collision if needed..

i guess with the sensor i will have to set a variable to true on begincontact and then to faose on endcontact .. And then i run into the endcontact is not send bug again.. ;)

Link to comment
Share on other sites

Just be aware that by testing for a single point, you will miss situations like low ceilings with a gap above them - you ideally need to 'sweep' the 40 pixels up from your character to detect what's in the way, rather than testing a discreet point. A raycast would give you that sweep, as would a tall collider. It's implementation dependent of course, if your platforms are all 'deep' or you'd not have situations where you'd have floating platforms etc. you'd be fine.

Link to comment
Share on other sites

In my game, I work with a sensor array (which ironically enough aren't sensor shapes; I disable their collisions in presolve but I need the full overlap data so I can't make them sensors). It's currently made up of ground feelers ("feet" that point into the floor) and a forward sensor that is always oriented to where the player is going. Ceiling and walls aren't as important for me right now, so I use the player body for these. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...