Hi All!
I use script below in my 2d game (video below)
to find closest enemy within weapon range (line > distance = ShipMissileRange * ShipMissileRangeMult;)
Now I want to draw a circle with that range around player so that player could see weapon range.
How to do that best way (preferably with texture)?
GameObject FindClosestEnemy()
{//user for ship's missile
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
if (gos.Length > 0)//at least 1 still exists
{
GameObject closest = null;// gos[0];
float distance = ShipMissileRange * ShipMissileRangeMult; //50;//max distance to find enemy //Mathf.Infinity;
Vector3 position = ship.transform.position;
foreach (GameObject go in gos)
{
EnemyScript es = go.gameObject.GetComponent<EnemyScript>();
if (es != null)//real enemy, not asteroid, etc
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
}
return closest;
}
else
{ return null; }
)

