Game Development Reference
In-Depth Information
14. Once
t
exceeds
1.0
, this signals to slide
nHead
(the start of the window) up
by
1
. Then, based on the playback mode, we either loop, stop, or handle the
end of curve scenario in another appropriate way. Rather than setting
t
back
to zero on a rollover, we subtract
1.0
instead. This way, we can capture the
difference if
t
ever ends up slightly greater than
1.0
but less than
dt
; it hap-
pens more than you'd think, and doing this results in a smoother and more
accurate curve:
if (t > 1.0f)
{
t -= 1.0f;
nHead++;
}
15. The
SplineMgr
class then translates the GameObject reference in
headObj
to the new position on the curve:
// update headObj
vOut = PointOnCurve(t, p0, p1, p2, p3);
if (HeadObj)
HeadObj.transform.position = vOut;
16. The
SplineMgr
class also translates a second GameObject along the
curve. The
TargetObj
object gets updated slightly in front of
headObj
and
is used by the NPC to face forward when walking along the curve:
// update lookObj
if (TargetObj)
{
Vector3 tgtPos = Vector3.zero;
tgtPos = PointOnCurve (t+dt, p0, p1,
p2, p3);
TargetObj.transform.position = tgtPos;
}
Congratulations, we have now written a robust spline system for our NPCs and other
moving objects! But how do we use it?