# [TowerDefense] How to show turrets range in game?

Updated on February 3, 2018 in
7 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
Reply
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
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
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
YOUR PREVIEW