Install Asset
Install via Godot
To maintain one source of truth, Godot Asset Library is just a mirror of the old asset library so you can download directly on Godot via the integrated asset library browser
Quick Information
A simple and optimized, static utility class for Godot 4+ to make 3D raycasting easier to use.
BZ Godot Physics
A clean, high-performance C# physics addon for Godot 4+. Simplifies low-level 3D raycasting with structured result containers and provides easy collision mask generation via extension methods.
β¨ Features
π― Easy Raycasting
- Extension methods for
Node3Dto perform raycasts and line casts - Clean, intuitive API that integrates seamlessly with your existing code
π¦ Structured Results
CastHitreadonly struct provides clean access to hit data- Hit position, surface normal, collider reference, and more
π Collision Masks
- Simple helper method to generate collision layer masks
- Uses 1-based layer numbers for intuitive usage
β‘ Lightweight
- Minimal footprint addon
- Pure Godot 4+ C# code
π Requirements
- Godot 4.0 or later
- .NET-enabled Godot build
- Basic C# knowledge
π¦ Installation
From Asset Library
- Search for "BZ Godot Physics" in the Godot Asset Library
- Click Install and enable the plugin in Project β Project Settings β Plugins
Manual Installation
- Download or clone this repository
- Copy the
addons/BZPhysicsHelperfolder into your project'saddons/directory - Enable the plugin in Project β Project Settings β Plugins
- Add
using BZ.Physics;to your scripts
π Quick Start
Cast a Ray
using BZ.Physics;
// Cast a ray from a position in a direction
CastHit hit = this.CastRay3D(
from: GlobalPosition,
direction: -GlobalBasis.Y,
distance: 100f,
layerMask: PhysicsHelper.GetCollisionMask(1, 2, 3)
);
if (hit.NonEmpty)
{
GD.Print($"Hit {hit.ColliderOwnerName} at {hit.HitPosition}");
}
Cast a Line Between Two Points
CastHit lineHit = this.CastLine3D(
from: startPoint,
to: endPoint,
collideWithAreas: true
);
Generate Collision Masks
// Create a mask for layers 1, 3, and 5
uint mask = PhysicsHelper.GetCollisionMask(1, 3, 5);
Note: Methods are available as extensions on any
Node3D. Ensure you haveusing BZ.Physics;at the top of your script.
π API Reference
CastHit Struct
A readonly struct containing raycast intersection data.
| Property | Type | Description |
|---|---|---|
NonEmpty |
bool |
true if the cast hit something, false otherwise |
HitPosition |
Vector3 |
The world-space position of the intersection point |
Normal |
Vector3 |
The surface normal at the hit point |
Collider |
CollisionObject3D |
The collider that was hit |
ColliderId |
uint |
The instance ID of the collider |
Rid |
Rid |
The low-level resource ID of the collider |
ColliderOwnerName |
string |
The name of the collider node |
PhysicsHelper Methods
CastRay3D
CastHit CastRay3D(this Node3D sender, Vector3 from, Vector3 direction, float distance, uint layerMask = uint.MaxValue, bool collideWithAreas = false)
Performs a 3D raycast from a starting point in a specific direction for a set distance.
Parameters:
from- The starting point of the ray in global spacedirection- The direction vector for the raydistance- The maximum distance the ray should travellayerMask- The collision mask to check against (defaults to all layers)collideWithAreas- If true, the ray will detect Area3D nodes as well
CastLine3D
CastHit CastLine3D(this Node3D sender, Vector3 from, Vector3 to, uint layerMask = uint.MaxValue, bool collideWithAreas = false)
Performs a 3D line cast between two specific points in global space.
Parameters:
from- The starting point of the line in global spaceto- The ending point of the line in global spacelayerMask- The collision mask to check against (defaults to all layers)collideWithAreas- If true, the ray will detect Area3D nodes in addition to bodies
GetCollisionMask
uint GetCollisionMask(params uint[] layers)
Generates a collision mask bitfield by combining the specified physics layer numbers.
Parameters:
layers- An array of 1-based layer numbers to include in the resulting mask
π Project Structure
BZ-Godot-Physics/
βββ addons/
β βββ BZPhysicsHelper/
β βββ PhysicsHelper.cs # Main utility file with extension methods
β βββ PluginMain.cs # Plugin entry point
β βββ plugin.cfg # Plugin configuration
βββ LICENSE # MIT License
βββ README.md # This file
π‘ Example Use Cases
- Ground Detection: Cast rays downward to check if a character is grounded
- Line of Sight: Cast lines between objects to check visibility
- Projectile Systems: Use raycasts for hitscan weapons
- Obstacle Detection: Check for obstacles in a character's path
π License
This project is licensed under the MIT License. See the LICENSE file for details.
π€ Contributing
Contributions are welcome! Feel free to:
- Report bugs via Issues
- Submit feature requests
- Create pull requests
β οΈ Notes
- This addon is for 3D physics only
- Uses C# - .NET-enabled Godot builds required
- Layer numbers are 1-based (matching Godot's editor UI)
- Remember to enable the plugin after installation
Made with β€οΈ for the Godot Community
A simple and optimized, static utility class for Godot 4+ to make 3D raycasting easier to use.
Reviews
Quick Information
A simple and optimized, static utility class for Godot 4+ to make 3D raycasting easier to use.