0 votes

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:
    if health <= 0:

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)

    if not is_on_floor():
        capncrunch.y -= gravity * delta
    move_and_slide(capncrunch, Vector3.UP)
Godot version 3.4
in Engine by (21 points)

1 Answer

0 votes
Best answer

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

by (167 points)
selected by
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 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 webmaster@godotengine.org with your username.