Reputation: 1
I have been following multiple sources to create my own custom StateMachine in Unity Engine, without any substates. I've managed to create a StateMachine that handles every basic, simple requirements of being a character in a video game such as Walk, Run, Crouch, Dodge, Turn and a single basic melee attack combo.
What I aim to do is: Create an Enemy or various player characters such as Mage, Wizard, Tank, Archer etc using this CharacterStateMachine as a Base. I also couldn't figure out how can I make this Character/Class become able to swap between multiple weapon sets without changing class? I've been thinking I should try and employ Polymorphism and Inheritance here to diverge from this base class.
I also wanted to use scriptable objects to keep things clean and stash damage numbers, health values, etc. for different classes/enemies there but that is a question for another post.
I'll attach my current States and StateMachine for basic visual understanding of states an average player/npc have in common and how many various states I have.
Copy pasting these states or statemachine everytime or rewriting every statescript with a different name doesn't sound efficient or logical so I reverted back to previous commit. The only way I can think of is deriving from the CharacterStateMachine (name of my StateMachine) and override some methods like Attack (plays a ranged attack animation and deals different damage numbers instead of melee attack damage) but I'm not sure how to do that exactly.
I have not gotten further and when I tried to do research based on my needs, I couldn't find a specific solution that I can follow along. So here I am.
Thanks for your time and input!
Upvotes: 0
Views: 89
Reputation: 26
I don't know if you are still trying to do this. But there is a relatively simple solution that you can work on to allow yourself to allow for classes without needing to add that much extra code. If you create a scriptable object for each type of class that contains the weapons it can use. By having an array of int values representing how it reacts to different situations you can then have each AI with an attached class request specific values from the array and add it to whatever value your calculating to see how much it affects the new state that they would transition to.
An example of the scriptable object can be as follows:
string className: /*Name of the class.*/
int classID: /*An int to more easily identify the class in the future.*/
weapon[] weaponAccess: /*All weapons the class can use.*/
int[] classStats /*All the stats for the class. So how far it will normally go, typical HP, and so on.*/ [health, walkingSpeed, runningSpeed, fearFactor]
In the attack state, you can have the AI do a quick check that determines which class it is and affects the appropriate values and how far it can attack. This depends on what variables you want to be affected by each class and how they change. It can then choose one of the weapons from the weapons array. This can be done at random or have it determine what weapon based on certain factors.
A sudo-code example for an attack state using info based on each class would be as follows:
attackState: characterBaseState
{
class currentClass = statemachine.currentClass;
weapon currentWeaponOut;
float minDist, maxDist;
chooseWeapon(){
currentWeaponOut = random.range(0,currentClass.weaponAccess.Length)
}
distanceToPlayer()
{
float dist = random.range(minDist + currentClass.classStats[3],maxDist + currentClass.classStats[3];
//The rest of the code to move to a new position.
}
}
The sudo code is based on what I think you currently have in the attack state but you can change the values and what you want to look at with each class and how it is meant to affect the states. Hope this helps with how you can solve the problem.
Upvotes: 0