The Hit struct is used to pass the details about an attack (such as the amount of damage being dealt) to the target being attacked. It uses the partial keyword to split its definition into two scripts:

Script Contents Portability
Hit.Core.cs The main parts of the system that are necessary for it to function. This file is intended to be directly reusable in any project without needing to modify it.
Hit.Details.cs Extra details like the amount of damage and the Team the attacker is on. This file is intended to be modified if you bring the system into another project and want to add more details such as damage types.

Using the system simply involves creating a new Hit and calling one of its methods. For example, Hit Triggers use something like this:

private void OnTriggerStay2D(Collider2D collider)
    var hit = new Hit(..., ..., ...);

Hit.TryHitComponent gets any component that implements the Hit.ITarget interface, checks if it CanBeHit by the current attack, and then calls ReceiveHit on it if allowed. There are various other TryHit methods which are listed in the API Documentation for Hit.

Since Hit is a struct, using it like this won't create any Garbage so its performance is good.

There are two components that implement Hit.ITarget:

Component Used By Description
Health Characters ReceiveHit reduces the Health.CurrentHealth by the Hit.damage and calls Health.OnCurrentHealthChanged.
Destructible Destructible Blocks Has no health, ReceiveHit simply destroys the GameObject.

Hit Modifiers

The Hit.ITarget methods take the Hit data as a ref parameter, meaning that they can modify the data for the caller. This is currently unused, but it allows for various possibilities if the system were to be used in a more complex game:

  • If a character has armour, it could reduce the Hit.damage so that afterwards the attacker could check how much damage was actually applied.
  • If a Paladin has an ability to intercept an attack targeting their allies, they could change the Hit.target during the Hit.ITarget.CanHit method.