Skip to content

Instantly share code, notes, and snippets.

@kimsama
Created December 27, 2012 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimsama/4385037 to your computer and use it in GitHub Desktop.
Save kimsama/4385037 to your computer and use it in GitHub Desktop.
WaitForAnimationTrigger class for AIBehavior, Unity3D package. It waits until the animation is done by the given ratio, and changes to the specified state.
using UnityEngine;
using System.Collections;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
///
/// WaitForAnimationTrigger class.
///
/// It waits until the animation is done by the given ratio, and changes to the specified state.
///
/// 2012. 12. @kimsama
///
public class WaitForAnimationTrigger : BaseTrigger
{
public float duration = 1.0f;
private Animation animation = null;
private bool isStarted = false;
///
/// Awake
///
void Awake()
{
animation = this.gameObject.GetComponent<Animation>();
if (animation == null)
animation = this.gameObject.transform.parent.GetComponent<Animation>();
}
///
///
///
protected override void Init()
{
this.isStarted = false;
}
///
///
///
protected override bool Evaluate(AIBehaviors fsm)
{
bool ret = false;
if (this.isStarted == false)
{
this.isStarted = true;
string name = "";
for(int i=0; i<fsm.currentState.animationStates.Length; i++)
{
name = fsm.currentState.animationStates[i].name;
if (animation.IsPlaying(name))
{
StartCoroutine(WaitForAnimation(name, this.duration, ()=>{
fsm.ChangeActiveState(transitionState);
ret = true;
}));
break;
}
}
}
return ret;
}
///
/// Wait for the animation is done till the given ratio.
/// The ratio has the value between [0, 1].
///
public IEnumerator WaitForAnimation(string name, float ratio, Action onComplete)
{
var state = animation[name];
return WaitForAnimation(state, ratio, onComplete);
}
///
/// Wait for the animation is done till the given ratio.
/// The ratio has the value between [0, 1].
///
public static IEnumerator WaitForAnimation(AnimationState state, float ratio, Action onComplete)
{
state.wrapMode = WrapMode.ClampForever;
state.enabled = true;
state.speed = state.speed == 0 ? 1 : state.speed;
var t = state.time;
while((t/state.length) + float.Epsilon < ratio)
{
t += Time.deltaTime * state.speed;
yield return null;
}
if (onComplete != null)
onComplete();
}
#if UNITY_EDITOR
// Implement GUI editor script for specifying animation ratio.
public override void DrawInspectorProperties(SerializedObject sObject)
{
SerializedProperty prop;
prop = sObject.FindProperty("duration");
if (prop.floatValue < 0.0f)
prop.floatValue = 0.0f;
if (prop.floatValue > 1.0f)
prop.floatValue = 1.0f;
EditorGUILayout.PropertyField(prop);
}
#endif
}
@dimmduh
Copy link

dimmduh commented Apr 9, 2020

why not just WaitFoSeconds(animationTime) ?

@kimsama
Copy link
Author

kimsama commented Apr 18, 2020

why not just WaitFoSeconds(animationTime) ?

I don't understand what WaitFoSeconds(animationTime) it is. Thus, it's been a looong time since this post. :-) Even I can't recall what AIBehaviors was. Sorry for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment