0
\$\begingroup\$

I have behaviors for my player such as attack, jump, dodge. I want these to act differently depending on the character the player has selected. I thought about making an abstract class for each of these actions, using an interfaces for each one might be a good way to go, but i'm not sure the best way to go about it within unity.

How do you handle abstracting behaviors like this within your projects? What are the pros and cons of the methods you use.

\$\endgroup\$
1
\$\begingroup\$

I would make one base abstract class that implements the interfaces you want all the characters to have (Attack, Jump, Dodge) etc.

Then extend each character from that base class and implement the interface behaviors which is what interfaces are for :)

That way if you need to get every object that can move you can do

List<IMoveable> moveableList = GameObject.FindObjectOfType(typeof(IMoveable));

I don't recommend using GameObject.FindObjectOfType(typeof(IMoveable)); itself just as an example because I dont want to write the code out for a raycast or sphere cast etc.

The point is you can refer to what might be a Enemy Class object just as a IMoveable if you need to move stuff or IAttack if you need to attack stuff, so while Player and Enemy are different classes they both share the fact they can move in common.

public interface IMove
{
    void Move(float xPos, float yPos);

}

public abstract class Entity : MonoBehaviour, IMove
{
    public int moveSpeed;

    public abstract void Move(float xPos, float yPos);
}




public class Mage : Entity
{
     //set moveSpeed in inspector to 2 for example
    public override void Move(float xPos, float yPos)
    {
        Debug.Log("I move " + moveSpeed +  " tiles at a time");
    }
}


public class Warrior : Entity
{
   //set moveSpeed in inspector to 1 for example
    public override void Move(float xPos, float yPos)
    {
        Debug.Log("I move " + moveSpeed +  " tiles at a time");
    }
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply. I could probably use events and listeners to find the object of type. \$\endgroup\$ – Pyreal Jun 12 '18 at 19:12
  • \$\begingroup\$ Yeah or sphere casts it depends on what you're doing at the time finding a target to hit with a missile, there is no real "correct" way to do these things, do what feels the most logical to you. \$\endgroup\$ – Pheonix2105 Jun 12 '18 at 19:18
  • \$\begingroup\$ I'm still trying to learn how to apply certain programming concepts in the context of unity. I'm all for simple and extendable. I think it's best to learn how others do it and see what method works best for me. \$\endgroup\$ – Pyreal Jun 12 '18 at 19:20
  • 1
    \$\begingroup\$ If you want more info on this type of design, do some research on the "Strategy pattern" \$\endgroup\$ – Zebraman Jun 12 '18 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.