using System; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.SceneManagement; public class AnimationObject { public readonly String name; public readonly float value; public readonly Coroutine coroutine; public AnimationObject(String name, float value, Coroutine coroutine) { this.name = name; this.value = value; this.coroutine = coroutine; } } public class Animate : MonoBehaviour { List animations = new List(); public void AnimateState(Animator animator, 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 AnimateState(Animator animator, string animVar, float animVal, float animationSpeed) { AnimationObject animationObject = animations.Find(item => item.name == animVar); if (animationObject != null && !Mathf.Approximately(animationObject.value, animVal)) { StopAnimation(animationObject); } StartAnimation(animator, animVar, animVal, animationSpeed); }*/ void StartAnimation(Animator animator, string animVar, float animVal, float animationSpeed) { Coroutine coroutine = StartCoroutine(AnimateCoroutine(animator, animVar, animVal, animationSpeed)); animations.Add(new AnimationObject(animVar, animVal, coroutine)); } void StopAnimation(AnimationObject animationObject) { if (animationObject.coroutine != null) { StopCoroutine(animationObject.coroutine); } animations.Remove(animationObject); } IEnumerator AnimateCoroutine(Animator animator, string animVar, float animVal, float animationSpeed) { float t = 0f; float beginningVal = animator.GetFloat(animVar); while (!Mathf.Approximately(animator.GetFloat(animVar), animVal)) { animator.SetFloat(animVar, Mathf.Lerp(beginningVal, animVal, t)); t+= Mathf.Clamp(animationSpeed * Time.deltaTime, -1f, 1f); yield return null; } } }