Table of Contents

Raycast

Problems

Let's say you need to use raycast and after following the official documentation you end up with this code:

var spaceState = GetWorld2D().DirectSpaceState;
var query = PhysicsRayQueryParameters2D.Create(Vector2.Zero, new Vector2(50, 100));
var result = spaceState.IntersectRay(query);

The result from a raycast query is of type Godot.Collections.Dictionary. While flexible, we would almost certainly need to access one or more of its properties.

Solution

Instead of writing:

var position = result["position"].AsVector2();
var normal = result["normal"].AsVector2();
GD.Print($"Raycast hit surface at {position} with normal {normal}.");

With RaycastHit2D you can write:

var hit = result.ToRaycastHit2D();
GD.Print($"Raycast hit surface at {hit.Position} with normal {hit.Normal}.");

Viewport Extensions

There are currently two 3D viewport extension methods regarding the use of raycast. These can be easily used by setting the project in Project > Project Settings... > 2D/3D Physics and defining your own PhysicsLayers3D enum.

Tip

Learn more about bit flags and bitwise/shift operators.

[Flags]
public enum PhysicsLayers3D : uint {
    None = 0,
    Ground = 1 << 0,    // Bit 0, value 1
    Player = 1 << 1,    // Bit 1, value 2
    Enemy = 1 << 2,     // Bit 2, value 4
    Obstacles = 1 << 3, // Bit 3, value 8
}

After setting up the physics layers, the following extensions can be used:

  1. Mouse raycast - getting raycast result from the current mouse position to specified physics layers

    var hit = GetViewport().GetMouseRaycast(PhysicsLayers3D.Ground | PhysicsLayers3D.Obstacles);
    
  2. Center raycast - getting raycast result from the center of the viewport to specified physics layers

    var hit = GetViewport().GetMouseRaycast(PhysicsLayers3D.Player | PhysicsLayers3D.Enemy);