Graphics Reference
In-Depth Information
myTransform= transform;
if(forceParent==null)
{
forceParent= myTransform;
}
The position and rotation of the parent transform will be used repeatedly in the weap-
ons setup code. To avoid having to look them up over and over again, their values are saved
in the variables TEMPvector3 and TEMProtation:
TEMPvector3= forceParent.position;
TEMProtation= forceParent.rotation;
When the game starts, the prefab references held by the ArrayList array are instanti-
ated and positioned at the parent transform's position.
The for loop uses weapons.Length to get the length of the weapons array and iterate
through it:
// we instantiate all of the weapons and hide them so that
// we can activate and use them when needed.
for( int i=0; i<weapons.Length; i++ )
{
The weapon at position i in the array is instantiated at the position of TEMPvector3
with the rotation from TEMProtation:
// Instantiate the item from the weapons list
TEMPgameObject= (GameObject) Instantiate( weapons[i],
TEMPvector3 + offsetWeaponSpawnPosition,
TEMProtation );
The instantiated weapon is then parented to the transform held in the variable force-
Parent so that it inherits its position and rotation. In this game, the layer of the parent
will also be used to identify where projectiles come from. We'll look at this layer-based
identification a little more as the function unfolds, but for now, just make a little note that
the layer of the weapon's parent will be copied down to the weapon. Individual weapons
scripts will also copy the layer setting onto their projectiles:
TEMPgameObject.transform.parent= forceParent;
TEMPgameObject.layer= forceParent.gameObject.layer;
TEMPgameObject.transform.position= TEMPvector3;
TEMPgameObject.transform.rotation= TEMProtation;
These new objects are going to be the ones that the script manages, enabling and dis-
abling them as required and calling on them to act as required. Each new weapon is added
to a new ArrayList called weaponSlots:
// store a reference to the gameObject in an ArrayList
weaponSlots.Add( TEMPgameObject );
The BaseWeaponScript instance attached to each weapon in those slots is found (using
GameObject.GetComponent()) and stored in another ArrayList named weaponScripts. By
putting them in an array, it saves having to look them up every time:
Search WWH ::




Custom Search