You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.5 KiB
93 lines
2.5 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Moloch
|
|
{
|
|
public class PhysicsOverlapManager : MonoBehaviour
|
|
{
|
|
public bool m_Gizmos;
|
|
public LayerMask m_LayerMask;
|
|
private Collider[] m_Colliders = new Collider[10];
|
|
|
|
void Awake()
|
|
{
|
|
m_Gizmos = true;
|
|
}
|
|
|
|
public Collider FindNearestTarget()
|
|
{
|
|
Array.Clear(m_Colliders, 0 , m_Colliders.Length);
|
|
|
|
if (CheckForOverlap())
|
|
{
|
|
return SortArrayByDistance();
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"No target found in area.");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private bool CheckForOverlap()
|
|
{
|
|
if (Physics.OverlapSphereNonAlloc(this.transform.position, 10f, m_Colliders, m_LayerMask) > 0)
|
|
{
|
|
|
|
foreach (Collider collider in m_Colliders)
|
|
{
|
|
if (collider != null)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private Collider SortArrayByDistance()
|
|
{
|
|
Collider shortestDistanceTarget = null;
|
|
float shortestDistance = 100f;
|
|
|
|
for (int i = 0; i < m_Colliders.Length; i++)
|
|
{
|
|
Collider col = m_Colliders[i];
|
|
if (col != null)
|
|
{
|
|
if (!CorpseManagerSingleton.Instance.CheckIfTaken(col.gameObject))
|
|
{
|
|
float currentDistance = Vector3.Distance(this.transform.position, col.transform.position);
|
|
|
|
if ((currentDistance < shortestDistance) && col.transform.position.y < 5f)
|
|
{
|
|
shortestDistance = currentDistance;
|
|
shortestDistanceTarget = col;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if(shortestDistanceTarget != null) Debug.Log($"{shortestDistanceTarget.name} is the closest target");
|
|
else if(shortestDistanceTarget == null) Debug.Log($"No target found.");
|
|
|
|
return shortestDistanceTarget;
|
|
}
|
|
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
Gizmos.color = Color.red;
|
|
|
|
if (m_Gizmos)
|
|
{
|
|
Gizmos.DrawWireSphere(transform.position, 10f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|