Check did just collide and subtract damage

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

Hi!
I dont know how to get information of what did the object hit or what code to add in objects settings, so that, when player object gets hit by a bullet, it would take some amount of HP off and then destroy it self.

:bust_in_silhouette: Reply From: henriquelalves

What you are looking for is collision detection, and as always, you have more than one way to do it - one of the simplest ones is by using PhysicsBody2D node (I’m considering here that you want to do that in 2D). You should check the Physics tutorial on Docs (http://docs.godotengine.org/en/stable/tutorials/2d/physics_introduction.html ), it should clarify the basics.

Worth mentioning is that you can use the PhysicsBody2D signals to get the effect you want of applying damage on collision - just connect the signal body_enter( Object body ) with a method in which you will test if the argument body is the scene you want.

That link doesnt work though…
: (
But ill check

Serenade | 2017-01-18 22:28

the link is adding a “),” at the end, just delete it and you are good to go, haha

henriquelalves | 2017-01-18 22:47

:bust_in_silhouette: Reply From: eons

When you detect the collision, you usually have a reference to the collider, that node, if has a “health” variable, the variable can be accessed with the dot notation like player.health, then you can work with it and assign values (this will trigger setters and getters if you made any).


The best approach could be adding all the objects to a group that can take damage and add to all of them the function that calculates the damage.

It can be damage(value) for direct damage, damage(bullet) or similar if the damage depends on what does the damage (like using the speed of the bullet), or hurt(damage) if your damage is a complex structure (like with effects or elements) or other, depends on the design.

The group is to abuse duck typing and avoid inheritance for interface-like stuff (you may need to damage siblings), it saves you from type-check too.


And check the shooter and platformer demos for some examples.