Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | DeadLy38 |
Hi,
I made a script where the enemy has a field of vision and if it sees the player, it will follow and rotate towards him.
But the enemy object just stops midway and only moves again when the player moves.
I also noticed that this happens when the enemy object moves in a straight line towards the player.
I checked every if statement and the other variables, but I just can not figure it out.
This is the code I wrote (It is in the enemy object):
extends KinematicBody2D
var speed = 50
var FOV = 45
var detection_radius = 400
var velocity = Vector2()
func _physics_process(delta):
var facing = Vector2(1,0).rotated(rotation).normalized()
var player = get_parent().get_node("Player")
var distance_to_player = player.global_position - global_position
if distance_to_player.length() <= detection_radius:
if rad2deg(acos(distance_to_player.normalized().dot(facing))) < FOV:
var direction = (player.global_position - global_position).normalized()
velocity = direction * speed
move_and_slide(velocity)
velocity = move_and_slide(velocity)
look_at(player.global_position)