Hi Guys!!
I’m building a shoot em up as a first project in Unity.
I have a Player GO, that has a WeaponMountGO and it instantiates a prefab laser. All is well.
I have now added 2 mounts for (L and R) and 2 more for Quad (L and R)
So, when single laser is on, the MainLaser fires
when Dual is on, MainLaser is off and the 2 inner mounts will fire,
and when Quad is on, the MainLaser is of but both Dual points and quad points fire (being 4 lasers)
Essentially, 3 states, Single, Dual and Quad.
Now I would like to as a power up that switches it from 1, 2 and 4.
I could go through a big bunch of IF statements with 3 bools but I really don’t think this is the right thing to do.
Thanks in advance guys!!!

Here is the fire code thus far, for Single only.
public void FireWeapon()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
laserShotSound.pitch = Random.Range(1f, 1.5f);
laserShotSound.Play();
laserFlash.Play();
laserSparks.Play();
GameObject MainLaserSpawn = (GameObject)Instantiate(mainLaserShot);
MainLaserSpawn.transform.position = mainLaserSpawn.transform.position;
nextFire = Time.time + mainLaserFireRate;
}
}
I don’t know how to make it short. You can try with the int value instead of using bool.
powerup:
public int weapon;
player:
int enabledWeapon = 1;
//....
if (Input.GetButton("Fire1") && Time.time > nextFire) {
if (enabledWeapon == 2)
{}
else
if (enabledWeapon ==4)
{}
else
//shoot one
//...
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.CompareTag ("PowerUp") {
enabledWeapon = other.gameObject.GetComponent<PowerUp>().weapon;
}
}
or replace if-statement with switch-case (and you can make different function for the different weapons and call it from the switch-case).
If the logic were 1 - 3 - 5 (all together), then i would take int value for the loop and use it in the weapons array.
for (int i = 0; i < enabledWeapon; i++) {
//shoot weapon[i]
//and you could use two ints in the powerup: one for the start weapon and one for the weapons count.
//for (int i = startWeapon; i < enabledWeapon; i++) {
//then 0 and 1 for main
//1 and 2 for 2
//1 and 4 for 4
//0 and 4 for all
}
1 Like
So I could have 1 as Single, 2 and Dual and 3 as Quad?
So I can use IF statements and use the ‘int’ as the condition?
IF (weaponArray == 1)
{
//Fire from main (Single Fire)
}
IF (weaponArray == 2)
{
//Fire from the Dual points (Dual Fire)
}
IF (weapomArray == 3)
{
//Fire from the 4 (Quad) points
}
Then when I get the powerUp have it set the ‘int’ to 1, 2, or 3?
Is it a bad idea to put this all in the FireWeapon() method? So everytime the player fires, it does all these checks? And tests what ‘Mode’ its in?
Thanks again man!! I really really really appreciate your time. 
I’ll show you what I did…I think as I’m a noob, there would be a more compact way of doing this…
public void FireWeapon()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
if (weaponMode == 1)
{
WeaponEffectsSingle();
GameObject MainLaserSpawn = (GameObject)Instantiate(playerLaserShot);
MainLaserSpawn.transform.position = mainLaserSpawn.transform.position;
nextFire = Time.time + mainLaserFireRate;
}
if (weaponMode == 2)
{
WeaponEffectsDual();
GameObject DualLaserSpawn = (GameObject)Instantiate(playerLaserShot);
DualLaserSpawn.transform.position = dualLaserL.transform.position;
GameObject DualLaserSpawn2 = (GameObject)Instantiate(playerLaserShot);
DualLaserSpawn2.transform.position = dualLaserR.transform.position;
nextFire = Time.time + mainLaserFireRate;
}
if (weaponMode >= 3)
{
WeaponEffectsQuad();
GameObject DualLaserSpawn = (GameObject)Instantiate(playerLaserShot);
DualLaserSpawn.transform.position = dualLaserL.transform.position;
GameObject DualLaserSpawn2 = (GameObject)Instantiate(playerLaserShot);
DualLaserSpawn2.transform.position = dualLaserR.transform.position;
GameObject QuadLaserSpawn = (GameObject)Instantiate(playerLaserShot);
QuadLaserSpawn.transform.position = quadLaserL.transform.position;
GameObject QuadLaserSpawn2 = (GameObject)Instantiate(playerLaserShot);
QuadLaserSpawn2.transform.position = quadLaserR.transform.position;
nextFire = Time.time + mainLaserFireRate;
}
}
}
Thats the weaponArray condition and result of each.
I have some particle effects (2 in fact on each shot) Is there a way to have these play if I put say, the 2 dual effects under an empty GO and have them fire at the positions of the 2 child object locations?
I’ll show you the effect code.
void WeaponEffectsSingle()
{
laserShotSound.pitch = Random.Range(1f, 1.5f);
laserShotSound.Play();
mainlaserFlash.Play();
mainlaserSparks.Play();
}
void WeaponEffectsDual()
{
laserShotSound.pitch = Random.Range(1f, 1.5f);
laserShotSound.Play();
dualLaserFlashL.Play();
dualLaserFlashR.Play();
dualLaserSparksL.Play();
dualLaserSparksR.Play();
}
void WeaponEffectsQuad()
{
laserShotSound.pitch = Random.Range(1f, 1.5f);
laserShotSound.Play();
dualLaserFlashL.Play();
dualLaserFlashR.Play();
dualLaserSparksL.Play();
dualLaserSparksR.Play();
quadLaserFlashL.Play();
quadLaserFlashR.Play();
quadLaserSparksL.Play();
quadLaserSparksR.Play();
}
Try to make every weapon as gameobject and start them from the players script.
players script:
{
//ok, you can put all weapon in the array or make them independently
public GameObject[] weaponArray;
//public GameObject weaponMain;
//...weapon left, right etc...
//maybe is it better to link directly to the fire script
//public WeaponScript weaponMainScript;
//then start it with: weaponMainScript.FireWeapon();
//or reference it in the start () from the gameobject
//void Start () {
//weaponMainScript = weaponMain.GetComponent<WeaponScript>();
//}
//...
public void FireWeapon()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
if (weaponMode == 1)
{
//take here the link to the needed weapon and call its fire script
weaponArray(0).GetComponent<WeaponScript>().FireWeapon ();
//weaponMain.GetComponent<WeaponScript>().FireWeapon ();
//weaponMainScript.FireWeapon();
nextFire = Time.time + mainLaserFireRate;
}
}
}
weapon script:
//empty gameobject for the laser spawn position or use Transform
//make the point child from this gameobject
public GameObject laserSpawn;
//then the links to particles, audio, laser, etc.. for this weapon
//...
//don't forget to make it public to call from the other script
public void FireWeapon() {
GameObject MainLaserSpawn = (GameObject)Instantiate(playerLaserShot);
MainLaserSpawn.transform.position = laserSpawn.transform.position;
laserShotSound.pitch = Random.Range(1f, 1.5f);
laserShotSound.Play();
mainlaserFlash.Play();
mainlaserSparks.Play();
}
at the end make prefab from this one weapon and use it as other weapons too (you should make it only bit more universal.
dualLaserFlashL.Play();
dualLaserFlashR.Play();
dualLaserSparksL.Play();
dualLaserSparksR.Play();
quadLaserFlashL.Play();
quadLaserFlashR.Play();
quadLaserSparksL.Play();
quadLaserSparksR.Play();
// public ParticleSystem[] laserSomething
//for (int = 0; i <laserSomething.Length; i++)
//{
//laserSomething[i].Play();
//}
//maybe with
//foreach(ParticleSystem effect in laserSomething)
//{
//effect.Play();
//}
1 Like
Oh I see. Some really good options there.
I’ll have to investigate how ARRAYS and the FOR EACH context works. I’d say it would be pretty useful sometimes.