0 votes

Hi,....so I have this.....but enemy moves away from the player, and i need to track the player when enemy's area trigger player (this works)
Thanks
enemy script :

extends KinematicBody

var enemy_speed = 20
var facing_speed = 180

func _ready():
pass

func _physics_process(delta):
var enemy_body = $enemy_area.get_overlapping_bodies()
var enemy_velocity = Vector3()
if enemy_body.size() != 0:
      for b in enemy_body:
            var pos = global_transform.basis.xform(Vector3(0,0,-1) * facing_speed)
            var plbody_pos = b.global_transform.origin
             if b.is_in_group("player"):
                   enemy_velocity = plbody_pos - pos
                   move_and_collide(enemy_velocity.normalized())
in Engine by (450 points)

1 Answer

+1 vote
Best answer

Tested, works. Enemy will follow player.

if enemy_body.size() != 0:
  for b in enemy_body:
        var pos = global_transform.origin
        var plbody_pos = b.global_transform.origin
         if b.is_in_group("player"):
               enemy_velocity = (plbody_pos - pos).normalized()
               move_and_collide(enemy_velocity)
by (381 points)
selected by

Thank you very much...it works great....you helped me a lot!
.....at the end of the script we can add.... look_at (plbody_pos, Vector3 (0,1,0))...for a facing/aim/rotate enemy's cannons for example.

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.