0 votes

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.

in Engine by (225 points)

2 Answers

0 votes

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.

by (304 points)
edited by

That link doesnt work though...
: (
But ill check

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

0 votes

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.

by (7,894 points)
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 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 webmaster@godotengine.org with your username.