How do I receive collision messages from a bullet hitting a wall?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gax
:warning: Old Version Published before Godot 3 was released.

I’m trying to make a 2D top-down shooter game. My bullet is currently a KinematicBody, and my wall is a StaticBody, which is embedded in a TileMap of many such walls. I want to be able to know when a bullet has hit a wall. I chose my bullet to be a KinematicBody because I expect that it should go at a constant velocity, and thus shouldn’t be affected by any forces, and I chose my wall to be a StaticBody because it should be stationary. However, it seems (based off of this) like you can’t get contact callbacks from KinematicBodies hitting StaticBodies. How should I best fix this issue?

:bust_in_silhouette: Reply From: quijipixel

You can get contact information from collisions from KinematicBodies to StaticBodies in a TileMap using KinematicsBodie´s collision API.

# _process in Bullet's Kinematic
func _process(delta):
    if is_colliding():
        print(get_collider())

This will print a TileMap object

Unfortunately, this doesn’t seem like it works for me. is_colliding() always returns false, even when the bullet is over the tile’s hitbox (I turned on Visible Collision Shapes).

gax | 2017-08-22 19:32

I’ve tried this and it worked. Can you share some code? maybe we are missing something here. Are the collision layers well set?

quijipixel | 2017-08-22 19:45

I’ve made a demo project here: https://ufile.io/dj0rs. The console should spam true when the projectile is over a wall, but for some reason, it doesn’t.

gax | 2017-08-22 20:35

The problem is that the CollisionShape2D in your KinematicBody2D bullet has the Trigger property enabled. If you read the tooltip that shows when you put the mouse over, you’ll read: “Set whether this shape is a trigger. A trigger shape detects collisions, but is otherwise unaffected by physics”. If you disable that trigger it should work.

quijipixel | 2017-08-22 21:07

Unfortunately, when i turn the Trigger property on, it changes the projectile’s velocity when it collides with walls and other projectiles. I’d like to have the velocity stay the same (which is how I interpreted ‘otherwise unaffected by physics’), but have the projectile tell me when it hits a wall anyway.

gax | 2017-08-22 21:13

To achieve that don’t use the collision of the KinematicBody, instead add a Area2D node to the bullet, and add a CollisionShape2D to the area, then connect the signal body_enter. Area2D won’t be affected by physics but it will notify you when there is a collision within its area.

quijipixel | 2017-08-22 21:17

Alright. I’m still not sure why a KinematicBody wouldn’t be appropriate here, but the Area2D approach works fine. Thanks!

gax | 2017-08-22 21:27