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:
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do…
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:
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.