enemy look at player when raycast hits (3d)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By xAkuma09

i have a simple ai for the enemy, that when im in his radius he looks at me and follows me. But i want to implement, if i shoot him he immediately looks towards me

my enemy code currently is:

extends KinematicBody

export var speed = 1000
var health = max_health
var max_health = 1200
var target
var space_state
var gravity = 100
var capncrunch = Vector3()
onready var player = "res://player.tscn"

func _ready():
	space_state = get_world().direct_space_state
	
func _process(delta):
	if max_health != health:
		pass
	if health <= 0:
		queue_free()

func _on_Area_body_entered(body):
	if body.is_in_group("Player"):
		target = body
		

func _on_Area_body_exited(body):
	if body.is_in_group("Player"):
		target = null

func move_to_target(delta):
	var direction = (target.transform.origin - transform.origin).normalized()
	move_and_slide(direction * speed * delta, Vector3.UP)

func _physics_process(delta):
	if target:
		var result = space_state.intersect_ray(global_transform.origin, target.global_transform.origin)
		if result.collider.is_in_group("Player"):
			look_at(target.global_transform.origin, Vector3.UP)
			move_to_target(delta)
			
	if not is_on_floor():
		capncrunch.y -= gravity * delta
	move_and_slide(capncrunch, Vector3.UP)
:bust_in_silhouette: Reply From: Midonk

I suppose that there is only one node in the “Player” group if your enemy got hit, you could do something like

target = get_tree().get_nodes_in_group("Player")[0]

This won’t stay very solid if you decide to add more than one player in your game though