Detecting Collision between CharacterBody2D and StaticBody2D

Godot Version 4.4

Question

This is my first time creating a game and learning the Godot engine, I’m working on a 2D Platformer right now with a character I control using a CharacterBody2D with move_and_slide(), I just finished up the basic movements with jumping and all that and want to move onto making the character actually take damage.

So I tried to create a test object with a StaticBody2D(It has its own scene) that I want to make it so that the character will take a hit point when it collide, only to find that the StaticBody2D don’t have anything I can use to detect collision.

After some fiddling I notice I can make the CharacterBody2D get the collider with get_slide_collision().get_collider(), which I see would return the object it collides with, but how should I go about identifying what kinda of objects it collides with? Like say I try to get its name it would just get the instanced object with a number at the end, I guess I can set it as a class but it doesn’t sit well with me(I’m not sure why exactly I just assume that I would use class if I only want it to be used for inheritance or smth), what other way can I go about it or is there a better way for me to detect collision or I should just ditch the StaticBody2D and opt for other kinds of nodes?

I think using class is fine, but if you don’t want to do that, you can place the StaticBody2D in a group and check if the collider is in that group.

tbh I do think I’m worrying a bit too much about doing things perfectly when I barely started learning, but thanks for telling me that!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.