0 votes

First, I am still working in Godot 2.1.4

I have two Kinematic Bodies one is a "monster" the other is the "Player" when they collide some calculations happen and who ever has the least amount of health "dies" and the player will get 1 point for defeating the "monster".

But the calculations happens for the duration of the collision and the "monster" only dies when they are not colliding anymore. So the player ends up with a huge amount of points for as long as he is colliding with the monster.

How can I check for the first collision response, do the calculations, slay the monster or get slain and not wait for them to stop colliding first.

Here is my code for check if they are colliding:

if is_colliding():
   if (get_collider().has_method("is_player")):
        if (get_collider().player_size >= monster_size):
            get_node("AnimationPlayer").play("monster_die")
            get_owner().get_node("Player").points_collected += monster_value
            print("monster killed")
        else:
            print("player killed")
else:
    print("This is not the player")

Any help will be appreciated, I am still new at this so please be gentle if code or format is not up to standard :)

in Engine by (53 points)

1 Answer

0 votes

If you only want to detect the first time that they collide, create an array in one of the nodes (in this case, the monster) that holds all the bodies that it is currently colliding with at the end of the step. Then in the next step, check whether the colliding body is found in the array. If not, it must have just collided.

Something like this: (I have only used Godot 3 so you may have to adjust it)

if is_colliding():
    if  (colliding_bodies.find(get_collider()) == -1):
    #if not found in the array
         if (get_collider().has_method("is_player")):
              if (get_collider().player_size >= monster_size):
                  get_node("AnimationPlayer").play("monster_die")
                  get_owner().get_node("Player").points_collected += monster_value
                  print("monster killed")
              else:
                  print("player killed")
    else:
         print("This is not the player")

#update array at the end of the step
colliding_bodies = get_colliders()
by (108 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 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 webmaster@godotengine.org with your username.