0 votes

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?

in Engine by (29 points)

1 Answer

+1 vote
Best answer

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

by (699 points)
selected by

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).

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

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.

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.

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.

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.

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

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.