Detect if an entity of the same type is above itself within a certain range?


(John Andersson) #1

I’m trying to make water, and if the player is in the water his ySpeed is negative. That works fine, but he bounces in and out of the water when he reaches the top. I want to make it so that the water object (I’m using grids here) detects if any other water object is above it in a certain range (within 2 pixels), if not, it’s a special top-water-object which makes the player’s ySpeed = 0.

How do I check this?


(JP Mortiboys) #2

If you’re using a grid for the water, I’d simply suggest you have two grids - one “deep water” and one “surface water”; the former making ySpeed = 2 and the latter ySpeed = 0. You could generate the surface water grid at runtime with a simple loop on entity creation, if you didn’t want to handle it at design time.


(John Andersson) #3

While that works, I could really use the whole “detection” approach, since that will allow for some nice effects in my game.


(Zachary Lewis) #4

You can invert your water grid and stop the player floating when he hits air. That way, when the player’s head pops out of the water, the “water grid” would collide with him and he’d stop floating.