Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Marmot |
I have a Super Mario mechanic where you jump on the heads of certain enemies to kill them. I have an ok system that, when an enemy and the player collide, checks if the player is currently falling and is not on the ground.
# When coming into contact with something, check if Player
func _on_Area2D_body_entered(body):
if body.get_name() == "Player":
# If Player is falling
if body.motion.y > 0 and body.is_on_floor() == false:
# Deduct health from self equal to Player's damage
hp -= body.dmg
# Make the player bounce
body.motion.y = -body.bounceHeight
# If health is less than or equal to 0, die
if hp <= 0:
body.getPoints(points)
queue_free()
else:
body.takeDamage(dmg)
It works, but I’d like a more precise way to check if the player is actually on top of the enemy, as now you can be just a pixel off of the ground and if you touch the enemy it counts as a hit and the enemy dies.
Thanks for reading!
Could you check to see whether the height of the player’s jump is almost equal to the enemy’s height? Maybe you could use that to figure out whether they played has hit the enemy.
Ertain | 2018-08-06 16:15
Some ideas not sure if they archive what you want.
Well you could check if the y cordinate of the player is around the area of the head of the enemy area. Allà body.position.y >= enemy.postion + enemy.height - offset
Or you could add a diffrent collision for the top part and see which one got activated first.
coffeeDragon | 2018-08-07 14:24
y not just add an area2d node on top of ur enemy make it collision one way and then
make use of the body_entered signal to check if a body enter the area
u can make it more precise by adding ur player to a group like"player" then when the signal is called check if the body belong to the group player and if it does your enemy is dead if not ur enemy lives and u dont worry about those pixel error
code | 2018-08-08 08:37