Hi
I am working on my first godot game and I am coding the ennemy.
Following this tutorial https://www.youtube.com/watch?v=aM8bRYH3_Po , I created the ray casting to check if my player is in sight of my ennemy. However the ray casting only detect my player when he enters the bottom part of the area2D wich represent the sight of my ennemy. Then after its been triggered once, I can go outside of its range so the ennemy stop, and re enter the Sight area from anywhere and it detect my player correctly.
Has anyone ever encoutered this behavior? I'm very confused I'm putting below my ennemy script if necessary.
extends KinematicBody2D
export (int) var speed = 10
var velocity = Vector2()
var player_in_range
var player_in_sight
onready var sprite = get_node("Ennemy")
onready var player = get_parent().get_node("Player")
var anim= "idle"
var target =null
func _physics_process(delta):
sightCheck()
sprite.play("idle")
if target && player_in_sight:
velocity = global_position.direction_to(target.global_position)
move_and_collide(velocity * speed * delta)
func _on_Sight_body_entered(body: Node) -> void:
if body == player :
player_in_range = true
target = body
print("player in range :", player_in_range)
print("player post",player.position)
func _on_Sight_body_exited(body: Node) -> void:
if body == player:
target = null
player_in_range = false
print ("player in range", player_in_range)
func sightCheck():
if player_in_range ==true:
var space_state = get_world_2d().direct_space_state
var sight_check= space_state.intersect_ray(position, player.position, [self],collision_mask)
if sight_check:
if sight_check.collider.name=="Player":
player_in_sight = true
print("PLayer in sight", player_in_sight)
else:
player_in_sight=false
print("Player in Sight :", player_in_sight)
EDIT : After more checks it seems like sightcheck returns null when my player is on the top part of the area2D (circle) wich represents the sight
The player position and ennemy position are updated correctly, the spacestate always return the same value. I have no idea how this variable can return null
Edit 2 : I found the solution and I'll leave it here if someone encouter the same type of problem, I had to resize the collisionshape of my player because the "ray", apparently the ray doesnt go throught the sprite of my player and so cant touche the collisionshape.