0 votes

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 detectradius
onready var Player = get
node("/root/Main/Player")
var rotationspeed = 1.5
var rotation
dir = 1
func ready():
$RayCast2D.add
exception($CollisionShape2D)
print("Ready to go!")
print(Player)

func physicsprocess(delta):
var EnemyToPlayer = Player.position - self.position
rotation += rotationdir * rotationspeed * delta
if EnemyToPlayer.length() < detectradius:
if acos(EnemyToPlayer.normalized().dot(Vector2(0, 1).rotated(rotation))) < .8:
$RayCast2D.enabled = true
$RayCast2D.cast
to = EnemyToPlayer
if $RayCast2D.iscolliding():
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 getinput():
velocity = Vector2()
if Input.is
actionpressed('uiright'):
velocity.x += 1
if Input.isactionpressed('uileft'):
velocity.x -= 1
if Input.is
actionpressed('uidown'):
velocity.y += 1
if Input.isactionpressed('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?

in Engine by (12 points)
edited by

Please log in or register to answer this question.

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.