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

Serialization and De-serializations

Delivering ReactJS applications at scale

GOJEK Engineer AMA Session with Jason Ganub

Guide to AtomicInteger in Java

Open Visual Studio Code from terminal Mac

Hack the Northeast: Behind the Scenes

List, Dictionary & Set Comprehensions (Python) in 4 Minutes

Creating a CloudFront using CLI

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

Player Jump in Unity

Understanding Frustum and Occlusion Culling in Unity3D

Creating Character Animations in Timeline and Tackling Some Known Issues

Learn to code by making a 2D space shooter game.