(3D)Help with a player tracking script

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

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())
:bust_in_silhouette: Reply From: Oen44

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)

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.

Bishop | 2018-09-20 21:44