namespace Test.Zinnia.Utility.Helper
{
using UnityEngine;
public static class RaycastHitHelper
{
///
/// Creates a blocker at a default position.
///
/// The blocker.
public static GameObject CreateBlocker(string name)
{
GameObject blocker = GameObject.CreatePrimitive(PrimitiveType.Cube);
blocker.name = name;
blocker.transform.position = Vector3.forward * 2f;
return blocker;
}
///
/// Generates dummy data.
///
/// The to block the ray.
/// Whether to destroy the blocker after the raycast simulation.
/// The origin point for the ray.
/// The direction for the ray to cast.
/// The valid hit data.
public static RaycastHit GetRaycastHit(GameObject blocker = null, bool cleanUpBlocker = true, Vector3? rayOrigin = null, Vector3? rayDirection = null)
{
if (blocker == null)
{
blocker = CreateBlocker("RaycastHitHelper");
}
if (rayDirection == null)
{
rayDirection = Vector3.forward;
}
#if UNITY_2022_2_OR_NEWER
Physics.simulationMode = SimulationMode.Script;
#else
Physics.autoSimulation = false;
#endif
Physics.Simulate(Time.fixedDeltaTime);
Physics.Raycast(rayOrigin.GetValueOrDefault(), rayDirection.GetValueOrDefault(), out RaycastHit hitData);
#if UNITY_2022_2_OR_NEWER
Physics.simulationMode = SimulationMode.FixedUpdate;
#else
Physics.autoSimulation = true;
#endif
if (cleanUpBlocker)
{
Object.Destroy(blocker);
}
return hitData;
}
}
}