How I destroy a enemy using raycast?

Godot Version



I hava a charactrer.tscn with pistol.tscn inherited and I want when the raycast is colliding with the area in my enemy and I shot it kill my enemy.

Pistol Script

extends Spatial

signal hit

onready var anim = $AnimationPlayer
onready var sound = $AudioStreamPlayer
onready var muzzle = $AnimatedSprite3D
onready var ray = $RayCast
var canShot = true

func _ready():

func _process(_delta):
	if Input.is_action_pressed("aim") and Input.is_action_pressed("fire") and canShot:
		if ray.is_colliding():
			var collision = ray.get_collider()

func fire():
	canShot = false
	muzzle.frame = 0"fire")"muzzle")

func _on_coldown_timeout():
	canShot = true
	pass # Replace with function body.

Zombie Script

extends KinematicBody

onready var nav = $"../Navigation" as Navigation
onready var player = $"../character" as KinematicBody
onready var anim = $AnimationPlayer

var path = []
var current_node = 0
var speed = 0.5

func _ready():

func _physics_process(delta):
	var movendo = false
	if current_node < path.size():
		var direction: Vector3 = path[current_node] - global_transform.origin
		var distance = direction.length()
		if distance < 0.01:
			current_node += 1
			var adjusted_speed = min(speed, distance)
			look_at(player.global_transform.origin, Vector3.UP)
			move_and_slide(direction.normalized() * adjusted_speed)
			movendo = true
	if movendo:"walk")

func update_path(target_position):
	var closest_point = nav.get_closest_point(target_position)
	path = nav.get_simple_path(global_transform.origin, closest_point)

func die():"death")

At this point I can hit the enemy only when click but I can`t destroy it

You’re not doing anything to the Zombie after you fire. In your pistol script, you should call die() and queue_free() if the collider is the area2d of a Zombie

1 Like