So i have weapons in my game, and each weapon does a specific function or attack (no melee weapons yet). I have an Attacks.cs script shown below. Player’s can place items into different hotbars, and the attack function is selected based on the items added. This is done through a bunch of if statements which is equal to the number of weapons in the game. I was wondering if there is a more efficient way of selecting weapon instead of hundreds of IF statements? As you can see below, the weapon name returns a function in ‘attackSelection’ which selects the attack. This is only done when the Player actually selects a different attack to be used, so isn’t run very often. I’d imagine when i reach 100+ weapons, those IF statements won’t look pretty but i can’t think of a better way to do it. Any ideas?
Attacks.cs below, a cut down version of the 550 line script with two attacks ‘ArcaneBlast’ and ‘SniperBlast’ to demonstrate how attacks work.
public void updateSelection(){
selected = GetComponent<PlayerHotBar>().selected;
selectedOff = GetComponent<PlayerHotBar>().selectedOff;
attackFunction = attackSelection(selected);
attackFunctionOff = attackSelection(selectedOff);
}
void Update(){
mana = player.GetComponent<PlayerStats>().mana;
try
{
if (Input.GetButton("Main") && !EventSystem.current.IsPointerOverGameObject()){
attackFunction();
}
if (Input.GetButton("Alt") && !EventSystem.current.IsPointerOverGameObject()){
attackFunctionOff();
}
}
catch
{
Debug.Log("No attack selected");
}
if (Input.GetKeyDown(KeyCode.Q)&& (mana > magicBallCost)) {
player.GetComponent<PlayerStats>().loseMana(magicBallCost);
spawnMagicBall();
}
if(currentZombies.Count > 0){
currentZombies.RemoveAll(GameObject => GameObject == null);
}
}
AttackMethod attackSelection(Equipment Equipped){
if(Equipped != null){
if(Equipped.name == "Wand_Stream"){
return Electric_Ball;
}
else if(Equipped.name == "Launcher"){
return missile_Homing;
}
else if(Equipped.name == "Staff_Lightning"){
return Lightning_Ball;
}
else if(Equipped.name == "Staff_Explosion"){
return Explosion;
}
else if (Equipped.name == "Staff_Holy"){
return karma;
}
else if (Equipped.name == "Staff_Vine"){
return ArcaneBlast;
}
else if (Equipped.name == "Staff_Undead"){
return ZombieAlly;
}
else if(Equipped.name == "Launcher_Seeker"){
return missile_Seeker;
}
else if(Equipped.name == "SpawnEgg"){
return throwEgg;
}
else if(Equipped.name == "Staff_Sword"){
return clusterBombFire;
}
}
return doNothing;
}
void doNothing(){
}
void ArcaneBlast(){
if(arcaneBlastOffCD == true){
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = (Input.mousePosition - sp).normalized;
GameObject objectInstance = Instantiate(arcaneBlast,
gameObject.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
objectInstance.GetComponent<Rigidbody2D>().AddForce(dir * 300f);
arcaneBlastOffCD = false;
WaitAndDo(arcaneBlastAS, () => arcaneBlastOffCD = true);
}
}
void SniperBlast(){
if(sniperBlastOffCD == true){
Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = (Input.mousePosition - sp).normalized;
GameObject objectInstance = Instantiate(sniperBlast,
gameObject.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
// Points missile at mouse pos
float rot_z = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
objectInstance.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 90);
objectInstance.GetComponent<Rigidbody2D>().AddForce(dir * sniperBlastForce);
sniperBlastOffCD = false;
WaitAndDo(sniperBlastAS, () => sniperBlastOffCD = true);
}
}