- Home /
Propogate upgrade to different entities (Wepon upgrade System)
I am making a top down shooter with an upgrade system. In The game,Every bullet has a "Damage" variable which tells how much damage it will do to the player. My question is what would be the best practice to apply the upgrade. Should the bullet query the "Game manager" about the upgrade and change its damage accordingly or if the gun should set the value everytime a bullet is fired ?
Answer by KevRev · Apr 21, 2019 at 12:16 PM
There's dozens of different ways you could do this, and your method could depend on how you instantiate the bullets in the first place.
I'll make up some theoretical variables, you can probably follow the logic and make something similar if you find it useful.
So firstly, lets do some initialisation:
**Player class**
public static int currentWeapon;
**Weapons class**
public static List<Weapon> weapon = new List<Weapon>(5);
public Weapon ()
{
public GameObject Model;
public int Damage;
}
Start()
weapon[0].Model={your weapon 1 prefab};
weapon[0].Damage=10.0f;
weapon[1]={your weapon 2 prefab};
weapon[1].Damage=20.0f;
weapon[2]={your weapon 3 prefab};
weapon[2].Damage=30.0f;
weapon[3]={your weapon 4 prefab};
weapon[3].Damage=40.0f;
weapon[4]={your weapon 5 prefab};
weapon[4].Damage=50.0f;
In your firing bullets code:
GameObject bullet = Instantiate(Weapons.weapon[Player.currentWeapon].Model);
In your OnCollision code:
OnCollission (Collider col){
shields -= col.transform.GetComponent<Weapon>().Damage;
}
This is a kinda high level overview of the logic I would suggest, not necessarily all working code as i've just written it off the top of my head.
But I think it'll give you something to work on.
Thanks for the reply.I have figured out that part actually.I may not have structured my question correctly but my question is that i have a upgrade system which increased the damage of the wepon.For example, for every level increase the damage is increased by 10%.How will i tell every bullet that the their damage has increased and they have to damage player more ?
Also, regarding the code :- is the OnCollision you are referring is on the wepon or on the player ?
I would just add a public float $$anonymous$$ultiplier; to the Weapon class.
By default it should be 1.0f;
Each time you collect a powerup, perform:
weapon[n].$$anonymous$$ultiplier+=0.1f;
Each time you die, set it to 1;
Then change your collision logic to:
Weapon bullet = col.transform.GetComponent<Weapon>();
shields -= bullet.Damage * bullet.$$anonymous$$ultiplier ;
And this collision logic should be placed on anything you can hit with the bullets (enemies / obstacles).
Thanks for the answer and very fast at that.It gave me some idea about how to proceed.