This project provides a modular Raycast Controller system in Unity to simplify detecting and responding to raycast interactions with colliders. The system is intuitive, extensible, and integrates seamlessly with the Unity Editor.
The Raycast Controller system is designed to detect and handle collider interactions using Unity's raycasting capabilities. It provides:
- Flexible raycasting configuration for distance, layers, and runtime control.
- Event-based callbacks (
OnEnter,OnStay,OnExit) for interaction logic. - Debugging tools to visualize raycasts and hit points in the Scene view.
The RaycastController.cs script is a self-contained system for raycasting in Unity. It supports real-time detection and event-driven behavior for collider interactions.
Key Features:
-
Customizable Raycasting:
- Configurable
distanceandlayerMaskfor fine-tuned detection. - Option to enable or disable raycasting dynamically with
castOnUpdate.
- Configurable
-
Event Handling:
OnEnter: Triggered when a new collider is detected.OnStay: Triggered when the ray continues to hit the same collider.OnExit: Triggered when the ray stops hitting a collider.
-
Debugging Tools:
- Visualize the raycast path, hit points, and hit sphere in the Scene view.
- Customizable debug colors for the ray, hit, and sphere.
Inspector Configuration:
- Configuration: Set the raycast
distance,layerMask, and whether to cast on everyUpdate. - Debug Options: Adjust the ray and sphere colors or enable hit sphere visualization.
- Events: Hook up UnityEvents to trigger custom behavior during interactions.
- Add the RaycastController script to any GameObject in your scene.
- Configure the script in the Inspector:
- Adjust the
distancefor ray length and specify alayerMaskfor target detection. - Choose whether the raycasting runs on every
UpdateusingcastOnUpdate.
- Adjust the
- Connect custom logic to the
OnEnter,OnStay, andOnExitevents in the Inspector.
- Attach the RaycastController component to any GameObject.
- Configure its properties in the Inspector:
- Set
distanceto define the maximum ray length. - Assign a
layerMaskto limit detection to specific layers.
- Set
- Connect UnityEvents (
OnEnter,OnStay,OnExit) to desired functions or behaviors.
- Enable debugging in the Inspector to visualize:
- The raycast path with
rayColor. - Hit points using
rayHitColorand a sphere at the hit location withsphereColor.
- The raycast path with
- Adjust
hitSphereSizeto scale the debug sphere at hit points.
This project is licensed under the MIT License. See the LICENSE file for details.