I’m working on a “TransitionStates” class that also changes items’ position based on the state of the character.
The order of events goes:
character switches animation controllers, the relevant animation plays, object changes position.
In order to make the item’s transition be timed with the Equip/Unequip animations, I’ve made the actual change in the item’s transform be a method that’s tied to an animation event within the Equip/Unequip animation.
This works brilliantly, but it’s prone to glitches: basically if the animation interrupts before the animation event’s time, the object’s transform won’t get updated, but anything else will get updated, so you’d get a character swinging fists with a sword the glitches from it’s arm/swinging a sword actually in it’s scabbard.
Anyway, since I do want some animations to interrupt the Equip/Unequip animations (such as getting hit, falling, dying etc), I don’t want to make the quip/unequip be completely un-interuptable (nor do I know how). However, I do want the object’s transform to update according to it’s state no matter what, but ONLY after the animation event’s time reaches, otherwise the whole animation looks bad.
Any ideas?
obviously events are not called from an animation if it is not playing anymore.
i think your options are:
-as you mentioned, not allowing those animations to be interrupted if they have events that are crucial for the game logic.
-controlling the events differently than through animation events. (you could for example start a coroutine that triggers something at the right time, for a coroutine you can decide yourself if it should always happen or if it can also be interrupted and stopped).
-another thing that came to my mind, if you really need to stick to the system with animation events that you have right now is using layers in your animator. then you could still have your old animation playing and triggering events while a new animation on a different layer plays and you switch the weights to the new layer. might be a bit hacky but could work
Have you considered using a StateMachineBehaviour to run your event when the state exits? I’ve never really used them, but it should be possible.
You might also be interested in Animancer (link in my signature) which gives your scripts full control over your animations so something like this would be really easy. The Equip State in the Weapons example currently cancels the weapon swap if it gets interrupted, but all you would need to do is add an OnExitState method similar to its OnUnequipEnd method:
public override void OnExitState()
{
base.OnExitState();
if (_Weapon != _EquippingWeapon)
{
DetachWeapon();
_Weapon = _EquippingWeapon;
AttachWeapon();
}
}