I'm making a stealth game about robots that track you. When the player enters the field of vision, the robots starts going after you (unimplemented yet) , but the robot don't track the player fully as you can see in the image.
I'm not a experienced programmer and pieced this code together from tutorials and documentation from the internet, so I'm kinda lost for what would be the problem. It could probably have to do with the dot product or not.
Variables:
var player_in_range = false
var awareness = 0
var enemy_rot = Vector3 (0,0,0)
var theres_player = false
onready var enemy = get_node(".")
onready var player = enemy
onready var raycast = $RayCast
Code in Question:
func detect_player(facing_dir, enemy_pos):
if theres_player:
var player_pos = player.transform.origin
var dir_to_player = enemy_pos.direction_to(player_pos)
if dir_to_player.dot(facing_dir) > .5 and player_in_range:
if awareness < 1 :
awareness += player.detectability
$AnimatedSprite3D.modulate = Color(1,1,1).blend(Color(1,0,0,awareness))
else :
if awareness > 0 :
awareness -= 0.1
$AnimatedSprite3D.modulate = Color(1,1,1).blend(Color(1,0,0,awareness))
if awareness >= 1 :
enemy_rot = facing_dir.cross(dir_to_player)
func enemy_movement():
enemy.set_rotation(enemy_rot)
for a reason I don't know of, the enemy don't keep looking at the player when reaching a certain angle.
Before getting in the field of vision.
Player detected.
When at this point, the robot stops looking at the player.
