For projectiles, use the pair of
Set Event Data on the
sender and
Get Event Info on the
receiver.
Brief: There is some condition (collision, trigger etc), and the sender knows about the receiver. Then, the sender sends an event over to the receiver, let’s say “HIT”. The receiver starts up, and with get event info reads a package of variables, like how much damage, from whom and so on.
Example. The weapon spawns (or switches on, or takes from pool) a damage dealer object, let’s call that
projectile (it doesn’t have to fly around, can also be the sword blade, or invisible object that only does the mechanics, while it looks whatever you like).
This projectile has some mechanics on it to make it move, shoot, fall down, or may do nothing at all. In any case, make a standalone state not connected to any other state. Then add a global event to it, and from the system event list pick
On Collision Enter or
On Trigger Enter. Depending on what you use. Also beware, the if you specifically use 2D physics, you always need to pick the corresponding 2D actions. That means, when it hits, or something passes through it, it will then start the new unconnected state and go from there.
In this state, you place the
Set Event Data, and a
Send Event. The data contains the values you want to ship over, and the send event is to let the target know, so it picks up that data. Send Event uses a global event, let’s call this “HIT”
Now the target has a health FSM. Inside, it also has a state with a global event, that waits for “HIT”. Then, place
Get Event Info, which makes it pick up the event data from the projectile.
Next, you want to process the data and run it through damage formula. The easiest is to substract from current health points. But you can add modifers before etcetera. After that you want to check if health is left and if not, do the death and despawn/respawn etc. If it was just a scratch, and everything is done, make sure to NULL out the variables that were used, i.e. set them to contain no relevant data to prevent that leftovers create bugs the next time.