Unity3d debugging trick

by

So Geek was just trying to track down a bug with calculating collisions between units in our game.  In order to track down the points he was calculating as part of the collision detection during move he used Gizmos in Unity3d speak.  This is a very handy feature.

So at the start of the script file he created a couple of variables:

public var gizmoPoints : Array;

function OnDrawGizmos()
{
  Gizmos.color = Color.red;
  for (var point : Vector3 in gizmoPoints)
  {
    Gizmos.DrawSphere(point, 0.21);
  }
}

Then in the in the method where he does the move path checking via Rigidbody.SweepTestAll there’s code like this:

...
gizmoPoints.Clear();
for (hit in hits)
{
  gizmoPoints.Add( hit.point );
  ...
}

So the idea is that for each point he found in from the sweep test gets added to the array and then if he has Gizmos turned on in the Unity3d Scene view he can see what points the sweep picked up on.  Geek also had it draw the point his algorithm had picked as the best point in a different color so he could see where in the swarm of possible points his algorithm was picking the best point.  Once he could visualize what was happening this way he found and fixed the bug in less than 10 seconds.   Very useful trick to help visualize what’s going on.  I’d used Gizmos once to actually draw the ray of a raycast as a debugging tool previously so this is now the second time this technique has proven useful.  Recommended.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: