Checking if a raycast hasn't hit anything. [C#]

Godot Version

v4.2.1.stable.mono.official [b09f793f5]

Question

I’m doing a raycast, and trying to check wether it has hit anything.
Code:

public object ShootRay(uint collisionMask)
	{
		Vector2 mousePos = GetViewport().GetMousePosition();
        var rayLen = 1000f;

		var from = cam.ProjectRayOrigin(mousePos);
		var to = from + (cam.ProjectRayNormal(mousePos) * rayLen);

		var space = GetWorld3D().DirectSpaceState;
		var query = PhysicsRayQueryParameters3D.Create(from, to, collisionMask);
		var result = space.IntersectRay(query);
		if (result.IsEmpty()) return null;
		//GD.Print(result);
		return result;
	}

I get this error:

Error CS1929: ‘Dictionary’ does not contain a definition for ‘IsEmpty’ and the best extension method overload ‘GD.IsEmpty(byte)’ requires a receiver of type ‘byte

I don’t really understand how to use GD.IsEmpty(), or if it even applies to this situation. Can anyone explain to me how it works, or if there are any alternatives for checking wether the raycast hit something? (Also I’m pretty sure that returning the result as an object is wrong, so I would appreciate some help on that front too)

Does it work with result.IsEmpty? Without parentheses, there’s no such method but there is a property on a base type

It doesn’t

Error CS0428: Cannot convert method group ‘IsEmpty’ to non-delegate type ‘bool’. Did you intend to invoke the method? [C:\Users\utente\Desktop\Godot Projects\Godot Projects.csproj]

Then there’s probably not an appropriate method like this, it’s not in the Dictionary type, you might need to just say result.Count == 0

Edit: Indeed, see here

If it’s a RayCast3D, I believe you can simply do:
!Raycast.IsColliding()
to check if it didn’t collide with anything.

It isn’t a RayCast3D, it’s using the server directly

Thanks, that worked.

1 Like

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