0 votes

How can I tell the difference between what node I am hitting? The script is attached to the moving object and I need it to differentiate between the floor or the player nodes. Everything has been set up with Body > Sprite, Collider. I have tried both KinematicBody2D and RigidBody2D:

var bodies = get_colliding_bodies()
print(bodies)
for body in bodies:
    if body.is_in_group("Player"):
        print("here")
        #queue_free()
"""
#get_collider()
if is_colliding():
    print(get_collider_shape())
    #print(get_node("/root/Player"))
    queue_free()
"""

For the code above it throws an error at get_colliding_bodies saying: condition !contant_moniter is true. returned Array()

in Engine by (60 points)

1 Answer

0 votes

body_enter signal will tell you about each collision when they happen (you need contact monitor).

get_colliding_bodies gives you an array with the bodies colliding IF contact monitor is true (default false), the size of the array will be as large as the collision max count (default 0).

Kinematic bodies works different, these have a simple collision detection that works only when (after) using move or move_to during _fixed_process

Kinematic character and platformer demo have many examples.

by (7,934 points)

I suppose I can look into even more demos. I am aware of the functions. I just need to put it all together. Mainly for the scope of this question, I need to detect whether or not it is a "Player" node or a "Floor" node on the script attached to the moving object.

Once you have the collider (with rigid signal or kinematic function) or the array with colliders (rigid function) you can check in many ways.

I prefer to use groups (is_in_group) because is flexible.
If you have a good collision layer design, these can be used to distinguish the type of object too.
The node name or part of the name can be used too, also node class/parent class.

The best way may depend on your design.

I finally got the Player node and have access to it. However the get_colliding_bodies always turn up empty:

var bodies = get_colliding_bodies()
   for body in bodies:
        if body.is_in_group("Player"):
            print("Game Over")

The tree structure is below. The Meteor node is instantiated as get_tree().get_root().add_child(newMeteor) at run time. Here is the Tree Structure.

Solved it! Had to set set_max_contacts_reported() since it defaults at 0, explains why get_colliding_bodies array turned up empty! Then I used:

var bodies = get_colliding_bodies()
if player in get_colliding_bodies():
    print("Game Over")
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.