So I basically have enemies. I want them to be heading toward my “goal” object, unless a “defender” object is within a certain range of them, in which case they will head towards that instead.
Here is the method for finding the closest defender:
private Transform findClosestDefender(float range)
{
Transform currentDefender;
if(defenders.Count > 0)
{
for(int i = 0; i < defenders.Count; i++)
{
RaycastHit hit;
if(Physics.Raycast(transform.position, defenders[i].transform.position, out hit, range))
{
Debug.DrawRay(transform.position, defenders[i].transform.position);
if(hit.distance < nearestDistance)
{
nearestDistance = hit.distance;
currentDefender = defenders[i].transform;
}
}
}
}
return null;
}
However, when this is run, two things go wrong.
- the Debug.DrawRay doesn’t show up (making me think there is something wrong with how I’m doing the ray)
- findClosestDefender(10) returns null, even if a defender object is right next to the enemy.
Any ideas?