I have a quite simple task to do, if an enemy see the character of the player, the enemy move toward the character, and I want to do it with RayCast2D node inside the enemy scene, so I can detect obstacles between character and enemy
I wrote these lines of code for the enemy so that at every physic process frame the ray is cast toward the player´s character:
if ($RayCast2D.is_colliding()==true and $RayCast2D.get_collider().get_parent()==character):
print("I see you")
print("I cant see you but i remember where you were")
move_toward(target,delta) #a simple function to move the enemy
This is not working, the player is detected only if the character stands in front of the enemy. I suppose the .setcastto() command is not working as intended, and that detection happens only if the character move inside the original ray I created into the enemy scene.
What am I doing wrong here?
it turned out the input for the command "cast to" is always refereed in local coordinates, which means that if you instance the scene with the RayCast2d in a world scene, if the scenese rotates or move, it is very difficult to calculate the correct casting Vector2.
It is much easier to create a very long RayCast, and then set it´s rotation so that is always pointing toward the player.
if I substitute
$RayCast2D.rotation=get_angle_to(character.global_position)it works fine.
PS: (may I say that to use local coordinates to cast a ray is quite silly? It´s very likely a ray is used to get collision between objects of different scenes with different local coordinates. Is there a way to take that into consideration without let me doing hard trigonometry problems?)