Having trouble getting Field of View working for enemies.

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

I’m trying to get a “simple” game together, where there are enemies searching for the player, who must navigate the level without being seen. After looking at a few different tutorials and stuff, this is what I’ve pulled together for the enemy script (I’m still pretty new to Godot):

extends KinematicBody2D

export (int) var detect_radius
onready var Player = get_node(“/root/Main/Player”)
var rotation_speed = 1.5
var rotation_dir = 1
func _ready():
$RayCast2D.add_exception($CollisionShape2D)
print(“Ready to go!”)
print(Player)

func _physics_process(delta):
var EnemyToPlayer = Player.position - self.position
rotation += rotation_dir * rotation_speed * delta
if EnemyToPlayer.length() < detect_radius:
if acos(EnemyToPlayer.normalized().dot(Vector2(0, 1).rotated(rotation))) < .8:
$RayCast2D.enabled = true
$RayCast2D.cast_to = EnemyToPlayer
if $RayCast2D.is_colliding():
var collider = $RayCast2D.get_collider()
print(“i see”, collider)
if $RayCast2D.get_collider() == Player:
print(“I see you!”)

It works perfectly, when the player is within the field of view, below the enemy, but its as if the field of view doesn’t rotate when enemy is rotating. Theoretically that would be an issue of where I calculate the dot product of where he is facing (Vector2(0,1).rotated(rotation)) but I don’t know what it should look like.
Here is the player script. The player is simply a circle moving around:
extends KinematicBody2D

signal seen
export (int) var speed = 200
var screensize

var velocity = Vector2()

func _ready():
screensize = get_viewport_rect().size
pass

func get_input():
velocity = Vector2()
if Input.is_action_pressed(‘ui_right’):
velocity.x += 1
if Input.is_action_pressed(‘ui_left’):
velocity.x -= 1
if Input.is_action_pressed(‘ui_down’):
velocity.y += 1
if Input.is_action_pressed(‘ui_up’):
velocity.y -= 1

velocity = velocity.normalized() * speed

func _process(delta):
get_input()

position += velocity * delta
position.x = clamp(position.x, 0, screensize.x)
position.y = clamp(position.y, 0, screensize.y)

move_and_slide(velocity)

What am I missing?