Files
Click-PointRPG/Assets/Scripts/Utilities/ProximityUtility.cs

70 lines
2.2 KiB
C#

using UnityEngine;
/// <summary>
/// Utility class for proximity and distance-based calculations.
/// Centralizes common distance checking logic used throughout the game.
/// </summary>
public static class ProximityUtility
{
/// <summary>
/// Check if two objects are within a specified distance
/// </summary>
public static bool IsWithinDistance(Vector3 from, Vector3 to, float distance)
{
return Vector3.Distance(from, to) <= distance;
}
/// <summary>
/// Check if two Transform objects are within a specified distance
/// </summary>
public static bool IsWithinDistance(Transform from, Transform to, float distance)
{
if (from == null || to == null) return false;
return IsWithinDistance(from.position, to.position, distance);
}
/// <summary>
/// Check if GameObject is within a specified distance
/// </summary>
public static bool IsWithinDistance(GameObject from, GameObject to, float distance)
{
if (from == null || to == null) return false;
return IsWithinDistance(from.transform.position, to.transform.position, distance);
}
/// <summary>
/// Get the distance between two positions
/// </summary>
public static float GetDistance(Vector3 from, Vector3 to)
{
return Vector3.Distance(from, to);
}
/// <summary>
/// Get the distance between two Transform objects
/// </summary>
public static float GetDistance(Transform from, Transform to)
{
if (from == null || to == null) return float.MaxValue;
return GetDistance(from.position, to.position);
}
/// <summary>
/// Get the distance between two GameObjects
/// </summary>
public static float GetDistance(GameObject from, GameObject to)
{
if (from == null || to == null) return float.MaxValue;
return GetDistance(from.transform.position, to.transform.position);
}
/// <summary>
/// Check if object is in range and handle both close and far cases
/// </summary>
public static bool CheckRange(Vector3 from, Vector3 to, float minDistance, float maxDistance, out float distance)
{
distance = GetDistance(from, to);
return distance >= minDistance && distance <= maxDistance;
}
}