Advertisement
ZeronSix

Simple Mecanim Curve-Based Event System (Unity3D)

Sep 15th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class AnimatorEventSystem {
  5.     public float triggerTheshold = 0.8f; // event curve will never reach 1
  6.    
  7.     private Animator avatar;
  8.     private Dictionary<int, bool> eventState;
  9.    
  10.     public AnimatorEventSystem(Animator anim) {
  11.         avatar = anim;
  12.         eventState = new Dictionary<int, bool>();
  13.     }
  14.    
  15.     public void Update() {
  16.         // proceed events
  17.         if (avatar) {
  18.             foreach (KeyValuePair<int, bool> e in eventState) {
  19.                 if (avatar.GetFloat(e.Key) < triggerTheshold) {
  20.                     eventState[e.Key] = false;
  21.                 }
  22.             }
  23.         }
  24.     }
  25.    
  26.     public bool CheckEvent(int hash) {
  27.         if (avatar) {
  28.             if (!eventState.ContainsKey(hash)) {
  29.                 eventState.Add(hash, false);
  30.             }
  31.             bool result = (!eventState[hash] && (avatar.GetFloat(hash) > triggerTheshold));
  32.             if (result)
  33.                 eventState[hash] = true;
  34.             return result;
  35.         } else {
  36.             return false;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement