Add utils + refacto
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unheimlich.Utility
|
||||
{
|
||||
public static class RaycastHelper
|
||||
{
|
||||
private static readonly Vector3 ViewportCenter = new Vector3(0.5f, 0.5f, 0f);
|
||||
|
||||
public static bool RaycastFromCameraCenter(Camera camera, float maxDistance,
|
||||
LayerMask layerMask, out RaycastHit hit)
|
||||
{
|
||||
if (camera == null)
|
||||
{
|
||||
hit = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
Ray ray = camera.ViewportPointToRay(ViewportCenter);
|
||||
return Physics.Raycast(ray, out hit, maxDistance, layerMask);
|
||||
}
|
||||
|
||||
public static bool RaycastFromCameraCenter<T>(Camera camera, float maxDistance,
|
||||
LayerMask layerMask, out T component) where T : Component
|
||||
{
|
||||
component = null;
|
||||
|
||||
if (RaycastFromCameraCenter(camera, maxDistance, layerMask, out RaycastHit hit))
|
||||
{
|
||||
component = hit.collider.GetComponent<T>();
|
||||
return component != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool ThrottledRaycast(ref float timer, float interval,
|
||||
Camera camera, float maxDistance, LayerMask layerMask, out RaycastHit hit)
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
|
||||
if (timer >= interval)
|
||||
{
|
||||
timer = 0f;
|
||||
return RaycastFromCameraCenter(camera, maxDistance, layerMask, out hit);
|
||||
}
|
||||
|
||||
hit = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool ThrottledRaycastComponent<T>(ref float timer, float interval,
|
||||
Camera camera, float maxDistance, LayerMask layerMask, out T component) where T : Component
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
|
||||
if (timer >= interval)
|
||||
{
|
||||
timer = 0f;
|
||||
return RaycastFromCameraCenter(camera, maxDistance, layerMask, out component);
|
||||
}
|
||||
|
||||
component = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user