How to draw a circle with weapon range around player so that player could see weapon range?

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; }

)

1 Like

So, the best method would be to get your hands on a circle sprite created in a different software. This could then be imported and adjusted to the size you need.

When you import it, make sure to set it to Sprite/UI instead of the default texture. Then, make it a child of your ship.
If you have a behaviour script on the ship, you can use the new circle’s game object scale or even the sprite renderer sprite->size field and alter the size there.

I recommend scaling in case you also want to use a scircle collider on it as a trigger volume for whatever area the ship is in.

1 Like

Thanks,
How to calculate that circle scaling properly?

What would be the formula?
It should include
distance = ShipMissileRange * ShipMissileRangeMult
But how exactly?

1 Like

take a look - can this help You?
I wrote this example using your “FindClosestEnemy ()” function
and using variables: ShipMissileRange, ShipMissileRangeMult

Of course, for a game with a large number of objects it is better to use colliders / triggers, rather than searching FindGameObjectsWithTag(“Enemy”);

5548537--571210--RangeVisualisation_01.gif

the .unitypackage is here:
https://drive.google.com/open?id=1Kbvj2-_lq5epcDLcQ_-7KwXL0HQm0t9-

5548537--571207--2020-03-04_10-15-28.png

5548537--571210--RangeVisualisation_01.gif