Answer the question
In order to leave comments, you need to log in
Is it possible to use interfaces like this in unity?
Good day everyone!
I have an IAtack interface which has an Atack() method.
It is implemented by several MeleeAtack and RangeAtack classes. Which implement the attack logic for close units and distant units, respectively.
MeleeAtack and RangeAtack also inherit from MonoBehaviour.
The bottom line is that with such an architecture, I can throw one of the Melee or Range components and in the controller script
IAtack atack = GetComponent();
And this is how you get an attack, depending on the component thrown on the object.
Thus, I do not interweave unnecessary logic in one common class and I can transfer common methods to abstract ones if necessary.
But my heart feels that this approach is a bit strange (most likely because I have not seen anything like it anywhere). Can you give me some advice on how to organize this kind of code.
Answer the question
In order to leave comments, you need to log in
A normal approach, they do so much where, they just saw little code, so it seems strange. Moreover, it is used not only for the attackers, but also for the attacked, for example, they make the IDamageable interface and work with it when dealing damage, and the logic for dealing damage is hidden in a specific class.
PS Attack is spelled with two t's.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question