[TowerDefense] How to show turrets range in game?

Updated on February 22, 2018 in [A] Brackeys Courses
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on May 8, 2017

Hello!

I have a problem right now..

I finish the Tower Defense Course and i want more, so i decided to continue on my own since there’s no more videos on this. But i’m really new to unity and scripting, so forgive me if its in fact a simple thing to do.

And a thing that i really want is show the towers range to the player, so he can see it in the game and i don’t know how to do it in the script..

I read that apparently the “OnDrawsGuizmoSelected” can only be display in the editor..
So my idea was to create a cylinder with size on y axis about 0.0001, with a color and the alpha down, but i want to make it interractive with the range of different turret, so this cylinder can take the size of the current range selected.

Someone have an idea? I took any suggestion thanx you 😀

  • Liked by
  • StoneGamer
Reply
1 on May 9, 2017
You could store the towers pos as a vector 3 called “towerPos” and the players pos as a vector 3 called “playerPos” and then use Pythagoras Thereom to find the direct distance e.g.
float directDistance = Square Root( playerPos^2 + towerPos^2)

then you could just display this, I believe this should work!
Good Luck 🙂

on May 9, 2017

Okay i will try that, thanx for the suggestion 😀

Show more replies
  • Liked by
Reply
Cancel
3 on May 10, 2017

If you can’t solve it with the first suggestion, which is basically using a circle equation you can draw a line by using the line renderer and creating points around the turret position.These points you can get by using Math.Sin() and Math.Cos().

This is the mathematical equation:
x = sin(rad) * r
y = cos(rad) * r

You need to decide how many points you want to draw and divide 2pi by that number to get “rad”. I have also seen Math.Deg2Rad () somewhere online, but I have never used that myself.

on May 14, 2017

Okay, it looks both a little complicated, but i will try it thanx for the suggestion guy 😀

Helpful
on May 17, 2017

Actually it isn’t really. Maybe I just explained it badly. If so I am sorry.

on May 23, 2017

I know this is a bit old, but what about having a circle sprite that always faces the camera.  It would be a .png file to preserve transparency and then you can easily customize and iterate the look of it in Photoshop.  Then all you have to do is code the sprite to turn on when you need it and turn off when you don’t.

Any thoughts on this?  Am I over-complicating it?

Show more replies
  • Liked by
Reply
Cancel
0 on February 3, 2018

Use the line renderer – like https://stackoverflow.com/questions/13708395/how-can-i-draw-a-circle-in-unity3d

I am working on a 2D tower defense game and I adapted similar code to show the range of the turret before placing it on a node. Once placed the circle goes away.

    [Header(“Showing the range”)]
    LineRenderer line;
    public int vertextCount = 40;
    public float lineWidth = 0.2f;
    float radius;

   void Start () {
        line = this.GetComponent<LineRenderer>();
        if (!BuildManager.instance.canFollow)  // if the user has chosen the turret and placed it
        {
            InvokeRepeating(“UpdateTarget”, 0f, repeatRate);
        }
        if(BuildManager.instance.canFollow) // if the user has chosen the turret but not placed it
        {
            ShowRange();
        }
    }
 public void ShowRange()
    {
        line.widthMultiplier = lineWidth;
        line.loop = true;
        radius = range /2f;
        float deltaTheta = (-2.5f * Mathf.PI) / vertextCount;
        float theta = 0f;
        line.positionCount = vertextCount;
        for (int i = 0; i < line.positionCount; i++)
        {
            Vector3 pos = new Vector3(radius * Mathf.Cos(theta), radius * Mathf.Sin(theta), 0f);
            line.SetPosition(i, pos);
            theta += deltaTheta;
        }
    }
  • Liked by
Reply
Cancel