Graphics Reference
In-Depth Information
// grab a reference to the weapon script attached to
// the weapon and store the reference in an ArrayList
TEMPWeapon= TEMPgameObject.GetComponent<BaseWeapon
Script>();
weaponScripts.Add( TEMPWeapon );
The weapons need to start out disabled; otherwise, they will all appear on top of
each other and all active. They are disabled here using the GameObject.SetActive() Unity
function:
// disable the weapon
TEMPgameObject.SetActive( false );
}
Now that all of the weapons are instantiated, set up, and hidden, the last thing needed
for initialization is to set the default weapon slot to the first weapon (index of 0). To set the
slot, use the SetWeaponSlot() function:
// now we set the default selected weapon to visible
SetWeaponSlot(0);
}
When a projectile hits an enemy or collides with a player, the object being hit needs
to know where the projectile came from in order for it to know how to react to the col-
lision. In the example game Lazer Blast Survival , projectile identification is made simply
by layer—all player projectiles are set to layer 9, and all enemy projectiles are on layer
17. Set the layer on the gun-mounting-point object (the object that will be the parent to
the weapon), and this weapon script will use the same layer when assigning layers to the
projectiles.
Another method for checking where projectiles have come from is to assign each
player with a unique ID number and to use the SetOwner() function to tell the projectiles
who owns each one. This method is employed in the Interstellar Paranoids and Tank Battle
example games. The ID number is passed on to projectiles and stored in the script that
each projectile has attached to it, the ProjectileController.cs script:
public void SetOwner(int aNum)
{
// used to identify the object firing, if required
ownerNum= aNum;
}
The function SetWeaponSlot takes an integer to use as an index number to refer to
the weapons stored in the ArrayList named weaponSlots. The weaponSlots ArrayList will
be in the same order as weapons added to the original weapons array in the Unity editor
Inspector window.
The SetWeaponSlot() function starts out by making sure that the weapon we are try-
ing to set (the variable slotNum) is not the weapon that is already active (the variable
lastSelectedWeaponSlot). This is done to avoid duplicate calls to activate the same already
activated weapon:
public virtual void SetWeaponSlot (int slotNum)
{
Search WWH ::




Custom Search