I have an Animation that triggers the Function LeftReset
living inside my Animate class.
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
public class Animate : MonoBehaviour
{
private Animator _animator;
public void initialize(Animator animator)
{
_animator = animator;
print("Event subscribed: " + (OnLeftJabReset != null));
}
private void Start()
{
OnLeftJabReset += test;
}
public void AnimateState(string animVar, float animVal, float animationSpeed)
{
float t = Mathf.Clamp01(animationSpeed*Time.deltaTime);
float newVal = Mathf.Lerp(_animator.GetFloat(animVar), animVal, t);
_animator.SetFloat(animVar, newVal);
//animator.Play("Attack");
//print(animator.GetCurrentAnimatorStateInfo(1).IsName("Idle"));
}
public void TriggerAnimation(string triggerName)
{
_animator.SetTrigger(triggerName);
}
public event Action OnLeftJabReset;
//public UnityEvent onLeftJab;
void LeftReset(AnimationEvent animationEvent)
{
print("Event subscribed: " + (OnLeftJabReset != null));
OnLeftJabReset?.Invoke();
}
void test()
{
print("fhuisdofghbis");
}
}
I subscribe to it inside of my PlayerAttack Script here:
public class PlayerAttack : MonoBehaviour
{
private InputManager _inputManager;
private LayerMask _enemyLayermask;
private Animate _animate;
public void initialize(InputManager inputManager, LayerMask enemyLayermask, Animate animate)
{
_inputManager = inputManager;
_enemyLayermask = enemyLayermask;
_animate = animate;
if (_animate != null)
{
animate.OnLeftJabReset += HandleLeftJab;
print("Subscribed to OnLeftJabReset");
}
else
{
print("Animate is null in PlayerAttack.initialize");
}
}
public void Attack()
{
_animate.TriggerAnimation("Left");
}
public void HandleLeftJab()
{
print("heya");
}
}
Both get instantiated in my player like this on Awake:
_animate = gameObject.AddComponent<Animate>();
_playerAttack = gameObject.AddComponent<PlayerAttack>();
_playerAttack.initialize(_inputManager, enemyLayer, _animate);
_animate.initialize(_animator);
But I cannot get my HandleLeftJab to register. Any Help is greatly appreciated.