Graphics Reference
In-Depth Information
Having all of the gizmos, lines, and editor helper graphics, the same colors would
make life harder. For that reason, Unity provides the Gizmos.color function to set their
colors. It takes just one call to Gizmos.color (passing in a Color object) and everything
after that point will be rendered using it.
Gizmos.color=Color.green;
Gizmos.DrawSphere does exactly that. It draws a nice sphere at a set position and
radius like this:
Gizmos.DrawSphere(currentPos,2);
// draw the line between the last waypoint and this
// one
Gizmos.color=Color.red;
Gizmos.DrawLine(lastPos, currentPos);
Gizmos.DrawLine is another utility provided by Unity to draw a simple line between
two 3D vectors. The gizmo drawing system has a few more options available to make edit-
ing easier.
According to the Unity documentation, the Gizmo interface provides the following
functions:
DrawCube
Draw a solid box with center and size.
DrawFrustum
Draw a camera frustum using the currently set Gizmos.matrix for its location and
rotation.
DrawGUITexture
Draw a texture in the scene.
DrawIcon
Draw an icon at a position in the scene view.
DrawLine
Draw a line starting at from toward to.
DrawRay
Draw a ray starting at from to from + direction.
DrawSphere
Draw a solid sphere with center and radius.
DrawWireCube
Draw a wireframe box with center and size.
DrawWireSphere
Draw a wireframe sphere with center and radius.
As stated earlier in this chapter, the waypoints are used at one stage as respawn points
for vehicles that may be stuck or off track. Their rotations are also used to ensure that the
repositioned vehicle is facing in the correct direction; for that reason, we need the way-
points to face “forward.” To make sure that the rotations of our waypoints are correct, we
go through and point each one forward toward the next one:
pointT.LookAt(currentPos);
You can easily rotate a transform so that its forward points toward another trans-
form by using the transform.LookAt() function. It takes one or two parameters, the first
being the transform you want to point toward and the second an optional up vector.
Note that having this functionality happening all the time during OnDrawGizmos()
can make drag-and-drop editing difficult, as objects pivot around automatically when you
Search WWH ::




Custom Search