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 = getnode("/root/Main/Player")
var rotationspeed = 1.5
var rotationdir = 1
func ready():
$RayCast2D.addexception($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.castto = EnemyToPlayer
if $RayCast2D.iscolliding():
var collider = $RayCast2D.getcollider()
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 = getviewport_rect().size
pass
func getinput():
velocity = Vector2()
if Input.isactionpressed('uiright'):
velocity.x += 1
if Input.isactionpressed('uileft'):
velocity.x -= 1
if Input.isactionpressed('uidown'):
velocity.y += 1
if Input.isactionpressed('ui_up'):
velocity.y -= 1
velocity = velocity.normalized() * speed
func process(delta):
getinput()
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?