Unity 3 - Visual debugging using Gizmos PDF Print E-mail

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) :


	function OnDrawGizmos() {

	    Gizmos.color = Color.blue;

	}

	

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:


	   Gizmos.DrawRay(transform.position, transform.forward * 10);

	

You can also supply an actual Ray object instead of two Vector3, for example if you're doing Ray casting.

Other methods are:

  • DrawLine - Takes two Vector3 and draw a Line between them
  • DrawSphere - Takes center and radius and draw the related sphere
  • DrawWireSphere - Like DrawSphere but in Wireframe
  • DrawCube - Takes center and size and draw the related cube
  • DrawWireCube - Like DrawCube but in Wireframe
 

Add comment


Security code
Refresh