Hitbox - Maxodie/BattriKeepel2 GitHub Wiki

Hitbox class

The hitbox class is a class created to simplify the creation and maintenance as well as the collision behaviors of the Entities hitboxes. It is a System.Serializable class, meaning its Serialized Fields will display in the editor if it is in a MonoBehaviour class.

It has 2 methods you need to know in order to use it:

  • Init(Transform transform);
  • BindOnCollision(UnityAction action);

See Use -> Code for their respective uses.

Use

Code

When creating an entity, you will need to add a Serialized Field Hitbox

[SerializeField] private Hitbox m_hitBox;

Once that is done, you need to call its Init method and feed it the transform you want the hitbox to be bound to.

private void Start() {
    m_hitBox.Init(transform);
}

Finally, if you want this hitbox to be useful you'll need to bind it's m_onCollision UnityEvent. To do so you'll need to use the BindOnCollision method.

private void BindActions() {
    m_hitBox.BindOnCollision({method});
}

Following is an example of a hitbox implementation on a test entity:

public class TestEntity : Entity {
    [SerializeField] private Hitbox m_hitbox;

    public void Init() {
        m_hitbox.Init(GetEntityGraphics().transform);
        m_hitbox.BindOnCollision(HandleCollision);
    }

    private void HandleCollision(Transform transform) {
        // collision logic here
    }
}

Editor

In the Editor, the hitbox class looks like that:

image

The first field is the type of hitbox. You can chose between:

  • Circle
  • Rectangular Parallelepiped

image

Depending on the type you chose, you will have to fill some info to the hitbox. I'm sure you can figure it out tho.

⚠️ **GitHub.com Fallback** ⚠️