RayCast results in C#

Using v4.1.3.stable.mono.official [f06b6836a]

Trying to check what Node a raycast has collided with

	public bool CheckLOS(Node2D Target)
	{
		var spaceState = GetWorld2D().DirectSpaceState;
		var query = PhysicsRayQueryParameters2D.Create(this.Position, Target.Position);
   		var result = spaceState.IntersectRay(query);		

		Object Collider = result["collider"] as Object;		

		if(Collider == Target)
		{
			GD.Print("u wot m8");
			return true;
		}

		return false;			
	}
}

Seeing people do this very easily in gdscript with Collider.Name but haven’t seen any C# examples

Getting anything out of the results dictionary is doing my head in since they’re all Variants. Is there a way to cast these out to compare them with Scene level stuff? I can’t even cast results[“position”] to Vector2 since its not a nullable type

Okay instantly found a solution after posting (spent hours on this beforehand)

		if((GodotObject)result["collider"] == Target)
		{
			GD.Print("u wot m8");
			return true;
		}

I believe I was casting to the object system class intead of GodotObject

StackOverFlow post that helped me understand variants a little better

2 Likes

Damn, I’ve been searching for the solution for the past 6 hours. I’m so grateful that someone finally knows what’s up.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.