Raycasting problem

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By modacless

Hey !
I got a problem with my raycasting :

Indeed i get an empty dictionnary with the var result most of the time. The " var result" has values when the player’s position is above the object that sends the ray.


here is the part of the code :

extends KinematicBody2D

export var rad = 200
export var grav = 18

const UP = Vector2(0,-1)

var test = null
var move = Vector2()
var target = null
# Called when the node enters the scene tree for the first time.
func _ready():
	$Visibility.connect("body_entered",self,"bdy_ent")
	$Visibility.connect("body_exited",self,"bdy_exited")
	
	var shape = CircleShape2D.new()
	shape.radius = 200
	
	$Visibility/CollisionShape2D.shape = shape
	pass
	
func _draw():
	draw_circle($Sprite.position,rad,Color(50,100,150))
	pass

func _physics_process(delta):
	update()
	_move()
	if target :
		aim()
	pass
	
func aim():
	var space_state = get_world_2d().direct_space_state
	var result = space_state.intersect_ray(position,target.position,[self],collision_mask)
	print(result)
	if result:
		print(result.collider.name)
		if result.collider.name == "Player":
			$Sprite.play("Shoot")
		else:
			$Sprite.play("Idle")
		if(target.position.x > $Sprite.position.x):
			$Sprite.flip_h = false
		else:
			$Sprite.flip_h = true
	else :
		$Sprite.play("Idle")
	
func bdy_ent(body):
	if body.name == "Player":
		target = body
	pass

func bdy_exited(body):
	if target == body:
		target = null
	pass
	
func _move():
	if is_on_floor():
		move.y = 0
	else:
		move.y += grav
		
	#move_and_slide(move,UP)


func _on_Timer_timeout():
	print(position)
	if target:
		print(target.position)
	pass # Replace with function body.