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
shooting_ray_cast.force_raycast_update()
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
pass
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.