How to Visualize a Custom Path in Unity 3D

Sharing is caring!

If you have ever dealt with AI before, you will know that every little bit of debugging information will help. Or, if you have built some sort of way-point system, you will know that it’s difficult to keep track of all the points in a path.

That’s where this tutorial comes in. We will learn how to visualize a custom path in the Unity Editor.

Planning

Before we start writing the script, we need to define what exactly we want it do. It can be summed up with the two points below:

  1. Keep Track of Path Points
  2. Draw Lines Between Points

Pretty simple to plan for. Now we can actually write the code.

Writing the Code

Let’s start with the first of our Planning Points from the above step.

Keep Track of Path Points

First, we need to add a tag before our class that says to run it when the editor is not in play mode. Next, we need to define a list of Transforms to keep track of the points.

 

Drawing a Path Between Points

Now we will use a function called OnDrawGizmos. It is similar to OnGUI, but instead it is used for drawing custom Gizmos.

We start by telling it to use red as the color of the lines (any color will do). Then it checks if there are two or more path waypoints. If there is, it loops through every transform in pathWaypoints, drawing a line in between them.

 

Putting it Together

With both steps combined, we have a beautifully working path-tracer.

 

That ends this tutorial on how to draw a path between points in Unity. If you liked it, make sure to follow us on Twitter to get notified when we post next.