Graphics Reference
In-Depth Information
gravity, and mass, whereas velocity will immediately set it in movement at the correct
speed in the required direction:
// add some force to move our projectile
theProjectile.rigidbody.velocity= fireDirection *
projectileSpeed;
}
MakeProjectile() handles the creation of the physical projectile, taking a parameter of
the ownerID (to pass on to the projectile) and returning the newly instantiated projectile
transform:
public virtual Transform MakeProjectile( int ownerID )
{
The SpawnController script from Chapter 4 is used to instantiate the projectile through
its Spawn() function. For convenience, SpawnController.Instance.Spawn() takes the exact
same parameters as Unity's Instantiate function: the prefab reference to spawn, a Vector3
position, and a rotation for the spawned object.
The projectile to spawn is held in the variable projectileGO (typed as a GameObject);
the position is found by taking myTransform's position (the position of the weapon) and
adding to it the spawnPosOffset vector (a Vector3 intended to be set in the Unity editor
Inspector window). The rotation is taken from the weapon's rotation myTransform.rota-
tion, but the projectile will normally be created from the Fire() function and a new rota-
tion applied to it after the projectile is returned:
// create a projectile
theProjectile= SpawnController.Instance.Spawn( projectileGO,
myTransform.position+spawnPosOffset + ( myTransform.forward
* forwardOffset ), myTransform.rotation );
To set the projectile's layer, access to its gameObject is required since there is no
way to set the layer of a transform. theProjectileGO holds a quick temporary reference
to theProjectile.gameObject, and the next line sets its layer to the value held by myLayer:
theProjectileGO= theProjectile.gameObject;
theProjectileGO.layer= myLayer;
The projectile needs to know about the owner ID so that it can be identified during
a collision. Before the script can tell the projectile about it, it needs to use GameObject.
GetComponent() to find the ProjectileController.cs script instance
// grab a ref to the projectile's controller so we can pass
// on some information about it
theProjectileController= theProjectileGO.GetComponent<Projec
tileController>();
With the instance reference in theProjectileController, the next line tells the projec-
tile's script about its owner ID:
// set owner ID so we know who sent it
theProjectileController.SetOwnerType(ownerID);
Search WWH ::




Custom Search