Graphics Reference
In-Depth Information
direction held by aDirection and the owner ID in the variable ownerID also get passed on
to the FireProjectile() function as parameters:
// decrease ammo
ammo--;
// generate the actual projectile
FireProjectile( aDirection, ownerID );
This is where the Boolean variable isLoaded gets reset to false to delay firing for a rea-
sonable amount of time (set by the value held in the variable reloadTime):
// we need to reload before we can fire again
isLoaded= false;
CancelInvoke is called to make sure that there is never more than one Invoke call to
Reloaded() waiting to activate:
// schedule a completion of reloading in <reloadTime>
// seconds:
CancelInvoke( "Reloaded" );
Invoke( "Reloaded", reloadTime );
}
The FireProjectile() function is where the projectile gets instantiated. It was called by
that last function, Fire():
public virtual void FireProjectile( Vector3 fireDirection, int
ownerID )
{
The function MakeProjectile will do the work in getting a physical projectile into the
scene, but it returns a transform that this function can then use to set up with:
// make our first projectile
theProjectile= MakeProjectile( ownerID );
Now theProjectile contains a transform; the code uses the Transform.LookAt() func-
tion to align it along the target trajectory passed in via the parameter variable fireDirec-
tion. Since LookAt() requires a world position vector (as opposed to a direction vector), it
takes the projectile's current position from theProjectile.position and adds the fireDirec-
tion vector to it. This will adjust the new projectile's rotation so that its z -axis is facing in
the required direction of travel:
// direct the projectile toward the direction of fire
theProjectile.LookAt( theProjectile.position + fireDirection );
To send the projectile on its way, the projectile's rigidbody has its velocity set to the
required firing direction multiplied by projectilSpeed. Setting its velocity directly, rather
than applying forces, makes its movement more predictable; how velocity is affected by
applied force is strongly influenced by other properties of the rigidbody such as drag,
Search WWH ::




Custom Search