0 votes

I was trying to implement a kill feature where player can jump on top of enemies and enemy will die, I first calculated the y co-ordinate of the player and enemy.After that using if statement i checked if player's y co-ordinate<enemies y co-ordinate then I make the player die.But problem comes when run towards the player it also makes the enemy die. Here is my sample code.

if collision.collider.is_in_group('enemy'):
    var player_feet = (position + $CollisionShape2D.shape.extents).y
    print(player_feet)
    if player_feet < collision.collider.position.y:
        print(player_feet)
        print(collision.collider.position.y)
        collision.collider.take_damage()
        velocity.y = -200
    else:
        hurt()

Player Sprite position
Enemy SPrite Position

Is there any other approach to solve this problem or what should i change in my code?

Godot version 3.5
in Engine by (14 points)

1 Answer

+1 vote

I am using 3D, but maybe this applies to 2D as well.

The only thing I can think of is by using a downward-facing RayCast, the length of the player, that is only enabled when the player is jumping and turns off when they are not jumping.

When the player hits the ground, the RayCast is disabled so you won't kill the enemy while on the ground. The length of the RayCast can then be fine-tuned for smoother gameplay.

by (138 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.