using UnityEngine;
using System.Collections;
namespace MoreMountains.Tools
{
///
/// Use this class to offset an animation by a random range
///
[RequireComponent(typeof(Animator))]
[AddComponentMenu("More Mountains/Tools/Animation/MMOffsetAnimation")]
public class MMOffsetAnimation : MonoBehaviour
{
/// the minimum amount (in seconds) by which to offset the animation
public float MinimumRandomRange = 0f;
/// the maximum amount (in seconds) by which to offset the animation
public float MaximumRandomRange = 1f;
/// the layer to affect
public int AnimationLayerID = 0;
/// whether or not to apply that offset on Start
public bool OffsetOnStart = true;
/// whether or not to offset animation on enable
public bool OffsetOnEnable = false;
/// whether or not to self disable after offsetting
public bool DisableAfterOffset = true;
protected Animator _animator;
protected AnimatorStateInfo _stateInfo;
///
/// On awake we store our animator
///
protected virtual void Awake()
{
_animator = this.gameObject.GetComponent();
}
///
/// On Start we offset our animation
///
protected virtual void Start()
{
if (!OffsetOnStart)
{
return;
}
OffsetCurrentAnimation();
}
///
/// On Enable we offset our animation if needed
///
protected virtual void OnEnable()
{
if (!OffsetOnEnable)
{
return;
}
OffsetCurrentAnimation();
}
///
/// offsets the target animation
///
public virtual void OffsetCurrentAnimation()
{
_stateInfo = _animator.GetCurrentAnimatorStateInfo(AnimationLayerID);
_animator.Play(_stateInfo.fullPathHash, -1, Random.Range(MinimumRandomRange, MaximumRandomRange));
if (DisableAfterOffset)
{
this.enabled = false;
}
}
}
}