I’m trying to make code that executes when a Player collides with an enemy while moving downward.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jackson King

Uh… Care to elaborate? What have you tried? Do you have any code that sort of works?

Ertain | 2019-04-14 22:53

var area = get_node(“Area2D”).get_overlapping_bodies()
if area.size() != 0:
   if body.is_in_group(“Enemy”)
   if is_in_air = true || get_position.y() + 1 > body.get_position().y

This setup kind of worked, but if an enemy fell on me from above the Player would kill it and if a player jumped upwards into an enemy they enemy would treat that as a wall collision and turn around. In the best case scenario I’d only need the is_in_air part, but that variable is not working properly.

if not is_on_floor():
   if velocity.y < 0
      is_in_air = false
      is_in_air = true
   is_in_air = false

Jackson King | 2019-04-15 04:20

You could get the direction of the player’s vector, and then determine whether the player is moving down (i.e. towards the floor). You could look to the example in the documentation on Vector Math to possibly get the code for direction.

Btw, I recommend writing that last line as

if is_in_air or get_position.y() + 1 > body.get_position().y:

for readability.

Ertain | 2019-04-15 05:52