using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class AnimatorStateRevertPlay : StateMachineBehaviour {
public string normalizeTimeName = "NTime";
public string speedMultiplierName = "SpeedMultiplier";
public string nextStateName = "";
float ntime;
float speedMulti;
bool isInTransition;
float animPlaySpeed;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animPlaySpeed = 1.0f / stateInfo.length;
speedMulti = animator.GetFloat(speedMultiplierName);
ntime = speedMulti < 0 ? 1 : 0;
isInTransition = false;
}
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller)
{
if (isInTransition)
return;
//play process
speedMulti = animator.GetFloat(speedMultiplierName);
ntime += animPlaySpeed * speedMulti * Time.deltaTime;
ntime = Mathf.Clamp01(ntime);
animator.SetFloat(normalizeTimeName, ntime);
//exit
if(speedMulti < 0 && ntime==0)
{
if (!string.IsNullOrEmpty(nextStateName))
{
animator.CrossFade(nextStateName,0.1f);
isInTransition = true;
}
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class AnimatorStateRevertPlay : StateMachineBehaviour {
public string normalizeTimeName = "NTime";
public string speedMultiplierName = "SpeedMultiplier";
public string nextStateName = "";
float ntime;
float speedMulti;
bool isInTransition;
float animPlaySpeed;
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animPlaySpeed = 1.0f / stateInfo.length;
speedMulti = animator.GetFloat(speedMultiplierName);
ntime = speedMulti < 0 ? 1 : 0;
isInTransition = false;
}
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller)
{
if (isInTransition)
return;
//play process
speedMulti = animator.GetFloat(speedMultiplierName);
ntime += animPlaySpeed * speedMulti * Time.deltaTime;
ntime = Mathf.Clamp01(ntime);
animator.SetFloat(normalizeTimeName, ntime);
//exit
if(speedMulti < 0 && ntime==0)
{
if (!string.IsNullOrEmpty(nextStateName))
{
animator.CrossFade(nextStateName,0.1f);
isInTransition = true;
}
}
}
}