|Articles & Tutorials|
|Unity 3 - Visual debugging using Gizmos|
When debugging a game it's very helpful to have a visual representation of non-visual objects.
For example, when checking an AI script that let an NPC move through a group of invisible waypoints, it's quite useful to be able to see the position of waypoints.
To do that, we can use Gizmos. Let's see how they works!
To use them, drawing has to be done in either OnDrawGizmos or OnDrawGizmosSelected functions of the MonoBehaviour script. The difference between the two is that, while the first one is always executed, code in the OnDrawGizmosSelected is executed only if the object the script is attached is currently selected in the Hierarchy.
In a AI wander behaviour I wrote some time ago, I used gizmos to visualize the wandering area (the wire sphere) and the current waypoint (the white cube)
To activate them, just switch the Gizmos button on in the Game view:
So, first of all, we can choose which color to use (default is white) :
Let's see which Gizmos are available now.
Using DrawRay we can draw a ray from between two positions. For example, here I am drawing a ray 10 meters long in the forward direction of the game object:
You can also supply an actual Ray object instead of two Vector3, for example if you're doing Ray casting.
Other methods are: