Activate animation trigger on each ShootableWeapon Use

Grannyboy

Active member
I thought it would be cool to have a "kick back" animation of my Desert eagle every time I shoot. I made an animation and put it on a animator on my weapon that is a child of my Item gameobject on my Right hand in the hierarchy.
DesertEagleOnCharacter.PNG

I set this super easy Trigger in the animator which my thought was to trigger in the Shootable weapon script on the Desert eagle. But there is no proper way of setting that up there.
DE_trigger.PNG

How would you recommend that I set up this trigger to be fired each time I use the specific shootable weapon?
If I set the trigger to be fired on my characters Use ability the weapon will mantle when its holstered as well.

Is there a simple solution to this that I'am not seeing clearly?
 
There actually are no built-in ways of calling trigger parameters, but it would be easy enough to set that up in this case. You'd just need a custom ShootableWeapon and then activate your animator trigger when the item is used.

Something like this: (un-tested)

C#:
public class ShootableWeaponTrigger : ShootableWeapon
{
    Animator animator;
    
    protected override void Awake() {
        animator = m_Character.GetComponent<Animator>();
        base.Awake();
    }
    
    public override void StartItemUse(ItemAbility itemAbility) {
        animator.SetTrigger("MyTrigger");
        base.StartItemUse(itemAbility);
    }
}
 
There actually are no built-in ways of calling trigger parameters, but it would be easy enough to set that up in this case. You'd just need a custom ShootableWeapon and then activate your animator trigger when the item is used.

Something like this: (un-tested)

C#:
public class ShootableWeaponTrigger : ShootableWeapon
{
    Animator animator;
   
    protected override void Awake() {
        animator = m_Character.GetComponent<Animator>();
        base.Awake();
    }
   
    public override void StartItemUse(ItemAbility itemAbility) {
        animator.SetTrigger("MyTrigger");
        base.StartItemUse(itemAbility);
    }
}
Hmm, I get what you are after and I tried it out as is and with some minor adjustments but it becomes a bit wierd.

This script basically replaces the ShootableWeapon script on the Item. But my animator is on the model under my Item parent on my Hand gameOject.
And the custom shootable weapon class kinda makes the script hard to work with since all decriptions & structure etc goes away in the editor...

Is there any other way for a custom script to "listen" to the shootable weapon and activate the trigger?
Is there a possibility to add into the ShootableWeapon script the feature of listening to use event from that specific Weapon/script in an up coming update of the UCC? I can see this being usefull for way more cases than this one!

Sorry if im being confusing, I would be happy to clearify if needed!
 
Yeah I see what you mean. You could create a simple custom script that listens for the 'OnItemStartUse' event (more info here: https://opsive.com/support/document...ller/programming-concepts/events/event-names/) and does the triggering based on that. Something like:

Code:
using UnityEngine;
using Opsive.Shared.Events;

public class MyScript
{
    void OnEnable() {
        EventHandler.RegisterEvent<IUsableItem, bool>("OnItemStartUse", OnItemStartUse);
    }
    
    void OnItemStartUse(IUsableItem item, bool use) {   
        // if `use` is true and `item` is the relevant item, then call SetTrigger on animator
    }
    
    void OnDestroy() {
        EventHandler.UnregisterEvent<IUsableItem, bool>("OnItemStartUse", OnItemStartUse);
    }
}

I know what you mean though, I'll pass this on to Justin as a feature suggestion, it's not a bad idea at all
 
Top