New to scripting, need help on how to approach creating several attacks

Hi Unity,

I am new to scripting and trying to look at the best way to create a bunch of attacks for my game. My end goal is to make it easy to create new melee and ranged attacks and add them later without duplicating a ton of code.

With that in mind, can you let me know if the way I am approaching this makes sense, or if maybe there is a better way to do this?

Right now I have the following classes:

Basic Attack - sets some basic properties for attacks such as attackDamage, and attackName and has setters and getters for those.
BasicMeleeAttack - inherits from basic attack and adds an attack range property with setter getter.
BasicRangedAttack - inherits from basic attack, doesnt add any properties yet but will in the future.

Now I have a script called AttackCreator which looks like this:

public static class AttackCreator
{

	public static BasicAttack CreateNewBasicAttack(AttackType attackType)
	{
		BasicAttack thisAttack = new BasicAttack();
		
		if(attackType == AttackType.MeleeAttack)
		{
			BasicMeleeAttack mAttack = CreateNewMeleeAttack();
			thisAttack = mAttack;
		}
		
		else if(attackType == AttackType.RangedAttack)
		{
			BasicRangedAttack rAttack = CreateNewRangedAttack();
			thisAttack = rAttack;
		}
		
		return thisAttack;
	}
	
	private static BasicMeleeAttack CreateNewMeleeAttack()
	{
		BasicMeleeAttack  mAttack = new BasicMeleeAttack();
		mAttack.AttackDamage = 10;
		mAttack.AttackName = "Basic Melee Attack";
	}
	
	private static BasicRangedAttack CreateNewRangedAttack()
	{
		BasicRangedAttack rAttack = new BasicRangedAttack();
		rAttack.AttackDamage = 10;
		rAttack.AttackName = "Basic Ranged Attack";
	}
}

Then in my PlayerAbilities class, I say for example:

meleeAttack = CreateNewBasicAttack(AttackType.meleeAttack);

and then when I actually set up my method to attack a mob it might be something like:

if(distanceFromMob < meleeAttack.range)
{
enemyMob.gameObject.SendMessage("TakeDamage, meleeAttack.AttackDamage;)
}

Does this make sense or is this a terrible way to approach this? What changes might I make to make it simple to add specific attacks like shootAnArrow, or chargingSlash, for example. Just add those to the AttackCreator?

I guess I am just not seeing the whole picture yet and want to make sure I am on the right track…

Thanks!

I’m not a c#er but it certainly sound like you know what you’re doing. You could use an enum to select a specific type of attack from the Inspector. That would allow you to limit your script to just the functions for that type.

Perfect, yep that is exactly what I am doing. I have enums for melee attacks and ranged attacks and then I just put the names like FireArrow, or whatever. Ok sometimes I guess I just need a quick check to make sure I dont put in a ton of work and then it ends up being a terrible idea down the road :smile:

THanks!