Hi, I am new to Godot.
I am trying to make enemy archer, that will detect player when they enter Area2D and start firing at the player.
The problem that occurs is that the "spaceState.IntersectRay(query);" returns a value only when the player is to the left of the enemy.

Code______________

var spaceState = GetWorld2D().DirectSpaceState;
var query = PhysicsRayQueryParameters2D.Create(this.GlobalPosition, _player.GlobalPosition);
query.Exclude = new Godot.Collections.Array<Rid> { GetNode<Area2D>("Detection Radius").GetRid() };

var result = spaceState.IntersectRay(query);
if (result != null) 
{
				if (result.Count > 0)
				{
					var collider = result["collider"].ToString();
					if (collider == _player.ToString())
					{
						GD.Print("Shooting");
						GD.Print("Hit at point: ", result["position"]);

						ableToShoot = false;
						shootTimer = shootTimerReset;
					}
				}
}
  • Toxe and Jesusemora replied to this.
  • Fixed the issue.
    My AnimatedSprite2D and collisionShape2D were off center from my characterbody2D.
    This caused the intersectRay method to not collide with the collision shape.

    TheFurious3101 Go to the Godot editor main menu --> Debug and enable Visible Collision Shapes. Do you see anything that could interfere with the raycast?

    Turned on visible collisions, and disabled any potential raycast2D nodes that may interfere with the intersectRay method.
    Unfortunately the problem persists.

    TheFurious3101 please format your code correctly. select the code and press the </> button below.
    this is already hard enough being C#.

    my guess without looking at your code is your parameters is pointing in the wrong direction.

    print the player position, the caster position, and the result one after the other, then do the math. you are using C# so you won't have a problem doing some simple math

    Fixed the issue.
    My AnimatedSprite2D and collisionShape2D were off center from my characterbody2D.
    This caused the intersectRay method to not collide with the collision shape.