Creating a dotted line in Unity

An article from www.knowledgescoops.com

Kunal Tandon
2 min readSep 4, 2018

Being a game developer, I use Line Renderer a lot to draw lines with Unity. But when it comes to drawing a dotted line, I get all messed up with how can we use it.

I came across a lot of tutorials online that shows how I can draw a dotted line by using the Line Renderer in Unity, but none of them actually worked (at least for me).

But I really needed the dotted line in my game.

So I came to the solution of creating a dotted line with Unity using the Sprites, with each sprite positioned on a straight line in such a way that they resemble a dotted line.

Drawing a dotted line

Download the unitypackage here

I have created a simple script that you can use to draw a dotted line in Vector2

Usage

The usage of this script is really simple. Just attach the script to an empty game object in your current scene.

To draw the dotted line, use the following code snippet.
Step 1: Import the DottedLine namespace in your file.
Step 2: For every frame, you want to show the dotted line in the game, use the following function call.

DottedLine.Instance.DrawDottedLine(pointA, pointB);

Step 3: Modify the inspector values to make your line the way you want it to look.

You can use an expandable pool of gameobjects to get performance gains from this script.

The code is available on Github at this link. You can fork the code snippet & optimize this code to get it merged with a pull request to help other developers use this.

For more such articles, visit www.knowledgescoops.com

Want to read more articles like this?
Follow me on medium @kunaltandon.kt
Connect with me on LinkedIn:
https://www.linkedin.com/in/kunal-tandon/

--

--