0 votes

I want my enemy to look at the player, can someone write an example code?
Here is the Enemy script:
extends KinematicBody2D

var player = null
var Shootingplayer = "res://Player/Shootingplayer.tscn"
var move = Vector2.ZERO
var speed = 2

func process(delta):
var dir = Shooting
player.position - position
rotation = dir.angle()

func physicsprocess(_delta):
move = Vector2.ZERO

if player != null:
    move = position.direction_to(player.position) * speed
else: 
    move = Vector2.ZERO

move = move.normalized()
move = move_and_collide(move)

func onArea2Dbodyentered(body):
if body != self and not body.isingroup("enemy"):
player = body

func onArea2Dbodyexited(_body):
player = null

func onArea2D2bodyentered(body):
if player == body:
gettree().changescene("res://Title + deathscreens/Deathscreen.tscn")
elif body != self:
queue_free()

Godot version latest
in Engine by (12 points)

1 Answer

+1 vote
if player != null:
    look_at(player.position)
    move = position.direction_to(player.position) * speed
by (5,188 points)
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 Frequently asked questions and 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 [email protected] with your username.