0 votes

Sorry if i cant express myself good in the title, but im making a little fps to practice with godot and im trying to make the shooting mechanic, i have a raycast and a kinematic body called "bag" with a variable called health, i want to make if the raycast detects a group called "enemies" in which the bag is, and if Input left_click pressed i want to substract 1 from that health variable, but i dont know how to do it, thanks for any help.

Godot version 3.3.2
in Engine by (37 points)

1 Answer

+1 vote
onready var shooting_ray_cast = get_node("RayCast") # where ever your RayCast is located

func _input(event: InputEvent) -> void:
    if event.is_action_pressed("left_click"): # replace with whatever your left click is in Project Settings
        var colliding_object = shooting_ray_cast.get_collider()
        if not is_instance_valid(colliding_object): # colliding_object is queued for deletion or does not exist
        elif colliding_object.get("health"):
            colliding_object.health = colliding_object.health - 1

This code should accomplish what you want to do, it's flawed though. Ideally you'd want to emit a signal when you get a valid collision_object and let the collision_object do the work of reducing its own health. You probably also want to create a Gun object and just call Gun.fire() when you get a left click input, then let the Gun object figure out if it can fire (isn't reloading, etc.), calculate the damage done, etc.

by (3,870 points)

Thanks for the answer, it worked!

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.