How I destroy a enemy using raycast?

Godot Version

3.5.3

Question

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():
	pass

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()
			print(collision.get_class())
		fire()
	pass

func fire():
	canShot = false
	muzzle.frame = 0
	anim.play("fire")
	muzzle.play("muzzle")
	$coldown.start()



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():
	pass

func _physics_process(delta):
	update_path(player.global_transform.origin)
	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
		else:
			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:
		anim.play("walk")
	else:
		anim.play("idle")
			


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():
	anim.play("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