This might be a Godot-Bug... (EDIT: Probably more a problem with floating-point-precision-stuff. normalize can't provide infinite-precise vectors.)
distance_to_player.normalized().dot(facing)
gives values > 1. (Something like 1.0000001)
acos does really NOT like values > 1
Try this:
extends KinematicBody2D
export (float, 1.0, 100.0, 1.0) var speed : float = 50.0
export (float, 0.0, 360.0, 1.0) var FOV : float = 45.0
export (float, 0.0, 1000.0, 5.0) var detection_radius : float = 400.0
var velocity : Vector2 = Vector2.ZERO
onready var player : KinematicBody2D = $"../Player"
func _ready() -> void:
FOV = acos(deg2rad(FOV))
func _physics_process(delta : float) -> void:
var facing : Vector2 = Vector2.RIGHT.rotated(rotation).normalized()
var distance_to_player : Vector2 = player.global_position - global_position
var direction : Vector2 = distance_to_player.normalized()
if distance_to_player.length() <= detection_radius:
if direction.normalized().dot(facing) > FOV:
velocity = direction * speed
velocity = move_and_slide(velocity)
look_at(player.global_position)
i optimized a little bit. Please ask, if something is unclear!
EDIT2:
You can debug suchs problems easily, if you just print out some variables while running your game.