Creating an AI Waypoint System on the Scene View by using OnDrawGizmos

Yesterday, I have spent some hours in searching the optimal way to guide my AI game objects along a curved path. First, I have tried to manually set a curve variable, which I optimized along the path to get a smooth AI run.

Secondly, I have tried setting some waypoints (transforms within empty game object) and updated the script to move with Vector3.MoveTowards(target).

Both of the solutions work but, I was not happy with the way I am doing it. Additionally, I was only able to observe the results during run time. If I notice a slight mismovement, I needed to take a note and update it in the sceneview and retest again. You can imagine how time consuming this might be.

Then I thought, why was I trying to rediscover mount Everest? The solution I am looking for was similar to a car AI. Despite I am not a fan of youtube tutorials, I have found out a great video by EYEmaginary. You can click the link below for a good explanation:

Using OnDrawGizmos method, we can create a waypoint system using the Scene View. For more information, you can check out the Unity documentation from the link below:

Here, please note that Gizmos are called on every frame.

The idea is pretty straightforward: Create an empty parent game object (call it Path or anything you want) and place the nodes as a child.

I have simply copied the script from the youtube video, I believe it is clear enough to understand:

Path Script — attached to the Path (parent) game object

Once you create a few nodes, you can add new nodes and arrange them within the Scene View:

Of course, you can increase the number of nodes for a smoother turn. You can observe that the last node is automatically is connected to the first node. Once I duplicate a node, it is being updated.

In order to make the lines only visible if we select the game object with the script attached, OnDrawGizmos method is updated to OnDrawGizmosSelected.

Adding the DrawWireSphere at the ending line enables us to observe the nodes on the scene view more easily:

Despite I doubt that this is a good solution for my case, I am happy to learn OnGizmos functionality in Unity.

--

--

--

Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Road To AWS SAA: High Availability Architecture

Solving Danny Ma’s SQL Case Study #2 Pizza Runner

How to install Debian in OCI

Provision k3s on Google Cloud with Terraform and k3sup — Nimble

Learn SQL Server Management Studio — Part 6 SSIS Packages

Top 10 Python Applications in the Real World You Need to Know

Python Rules Of Coding: Docstrings

Using PDAs and SPL Token in Anchor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ali Emre Onur

Ali Emre Onur

Unity Developer

More from Medium

My journey becoming a Unity game developer: Main Menu: Create and Animate the Main Menu

Creating a Mech Hangar Room

3rd Person Follow Virtual Camera Explained in Unity’s Cinemachine

Unity Profiler- Game Dev Series 144