How do i detect collisions layers/groups

Godot Version

4.4.1

Question

How do i detect if player gets hit by an enemy or lava to get insta kill him?
i had to create different colliders for that because interactions are different; having a lot of colliders ruins game performance.

If I understand you correctly - You want a way to check which source the enemy has taken damage from? e.g. if from small enemy = small amount of damage or if the player falls into lava they lose all of their hp?
Theres a lot of ways to do this

Can you give some more info on what you are trying to make, which code you have and your node setup?

I want to know the source from the damage???

If hit by enemy

El if hit by lava …

And do you only want two sources of damage?

There are many much easier and more optimal way sto do this but its kind of hard to help when You arent showing how your player is taking damage or how the enemy is dealing damage

without knowing anything about your setup

What i would do is:

add this to your player:

func hit(damage : float) -> void # this is how the player takes damage
     player_hp -= damage
     # AND ANY OTHER LOGIC YOU WANT TO TRIGGER WHEN THE PLAYER TAKES DAMAGE

add this to enemy script and/or lava script

var damage : float = some_value 
func on_body_entered(body )
if body.name == "Whatever the player name is"
      body.hit(damage)

i think adding the player to a group named “player” would be good. Then you could scale it to multiplayer, have it work for different characters or whatever you need.
If you check by groups you could add checks for enemies or other things as well if you want them to be able to get hurt.
example:
if body.is_in_group(“player”) or body.is_in_group(“enemy”):
effect

There’s a fair few ways your could do this:

Get the layer number of the collider, that’s the bitwise number. This is probably the most performant way as you are just checking a number, but the code wouldnt be very clear.

Get the class type, which is probably fairly quick too.

You could get the group name but that’s not very performant.

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