This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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
var rotationspeed = 1.5
var rotation
dir = 1
func ready():
print("Ready to go!")

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
to = EnemyToPlayer
if $RayCast2D.iscolliding():
var collider = $RayCast2D.get
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

func getinput():
velocity = Vector2()
velocity.x += 1
if Input.isactionpressed('uileft'):
velocity.x -= 1
velocity.y += 1
if Input.isactionpressed('ui_up'):
velocity.y -= 1

velocity = velocity.normalized() * speed

func process(delta):

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


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.