- Home /
Different enemy types script design
So I have 2 different enemy types for now. Both of them with EnemyAi, Health scripts tied to parent and EnemyAttack script tied to child damage zone trigger.
Currently the enemies are all melee and just have different damage and attack delay from EnemyAttack.cs. I wanted to add some abilities for the enemies so they may for example stun, slow you on attack; some may also have occasional projectile shooting towards the player; some may charge at the player. So where would it make sense I add the scripts for that? I had an idea of using enum on enemyAi, so EnemyAttack will use switch with the enum enemy type on attack method but I want to hear your opinions. Maybe separate script for each special ability would be good?
Answer by hacky97 · Jul 09, 2020 at 02:35 PM
a separate script for each ability will be very good. Let the scripts all inherit from a generic attack class, so you can add that class to the player:
public class stun: attack
{
}
I saw few suggestions about inheritance over the internet, but I'm not sure... what would it allow me to do? Would it make me remove EnemyAttack script from enemy because I could use Stun class with EnemyAttack's methods ins$$anonymous$$d? Is that how it works? I don't really get inheritance and I don't think it would work well with my player's attack because it uses different non melee loadout with switching weapons system
@kot2202 It would allow you get rid of the enum and have each decendent of attack handle its own code. In the beginning it will be more of a hassel, later, ehwn enemies can attack more than only the player and when the attacks types become more, you will be happy to have inheritance. It can be a bit hard to wrap your head around. Look at this video and try it with some basic classes first: https://www.youtube.com/watch?v=F7Wu6_uzD1I
So I understood it this way - I will recreate EnemyAttack.cs script into separate classes, because now it not only did the attacking but also trigger enter, exit detecting.
I will create separate detection script that calls [SerializeField] UnityEvent on triggers and I will attach the new 'special type attacking' script to the event. The special attacking script will inherit from basic attack script that all it does is deduct enemy hp + do some extra thing like stun on top of that from the special type attack script.
Is it correct solution? Events came to my $$anonymous$$d because I have no idea how would I access the different scripts of attack types, I wouldn't want to check 10 GetComponents, I have never used events before though
Yes, that sounds like the right way to go. I would have multiple special attack scripts like AttackStun, AttackSlow or AttackProjectile.
Your answer
Follow this Question
Related Questions
Kill character on impact 1 Answer
Why do the AI get caught on trees? 0 Answers
NavMeshAgent does not follow player 0 Answers
free roaming enemy ai 1 Answer