- Home /
Is there a smarter way to implement event sequencing?
Hi all Unity users,
I'm new to Unity and C# and been trying to get a hang on timing and sequencing using MonoBehavior.Update and Time.delta. I have the following code that chronologically triggers the change of animation at various specific times.
 using UnityEngine;
 using System;
 
 public class Sequencer: MonoBehaviour 
 {
 
     //Basic flags to indicate actions required by this class
     private bool m_StartPlayed;
     private bool m_FirstPlayed;
     private bool m_SecondPlayed;
     private bool m_ThirdPlayed;
     private bool m_EnderPlayed;
     private float m_TimeIntervalAccum;
 
     void Start()
     {
         m_StartPlayed = false;
         m_FirstPlayed = false;
         m_SecondPlayed = false;
         m_ThirdPlayed = false;
         m_EnderPlayed = false;
         m_TimeIntervalAccum = 0.0f;
         
         animation.wrapMode = WrapMode.Loop;
         animation.Stop();        
     }
     
     void Update()
     {
         m_TimeIntervalAccum += Time.deltaTime;
         if(m_TimeIntervalAccum > 1.0f)
         {
             //Perform interval tasks in Update(). This one call WorkOut every 1 sec
             //WorkOut();        
             m_TimeIntervalAccum = 0.0f;
         }
         
         if(Time.time > 0.0 && Time.time  4.0 && Time.time ] 30.0 && Time.time ]] 60.0 && Time.time  63.0)
         {
             if(m_EnderPlayed == false)
             {
                 animation.CrossFade("Idle_A3_ShiftLeft", 1.0);
                 m_EnderPlayed = true;
             }
         }           
     }    
 }
The above code works as a series of animation cross-fade calls occuring once at a particular time each. I would call it "pulse events" because I am not sure about the correct technical terms. The above implementation looks really amateurish and inflexible as I would need as many bool as number of pulse events. Is there a better way to implement this? I tried looking for information on actual C# delegates and events on google but can't find one specific to what I have shown here.
Since the question is already answered I'd just like to point out that m_TimeIntervalAccum = 0.0f; should be m_TimeIntervalAccum--; for your function to be run on average once a second. Your way it would always be called after little over a second, which can of course accumulate. ;)
Answer by testure · Jun 09, 2011 at 07:59 AM
Since you already know the amount of time you need between each animation, I would use a coroutine:
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
 public class example : MonoBehaviour 
 {
     IEnumerator Do() 
     {
       print("Do now");
       yield return new WaitForSeconds(2);
       print("Do 2 seconds later");
     }
  
 IEnumerator Awake() 
 {
   yield return StartCoroutine("Do");
   print("Also after 2 seconds");
   print("This is after the Do coroutine has finished execution");
 }
}
Thanks for the quick reply! The link you have shown really explain the things I need in details. I totally overlook that when reading the manual. By the way, what does the return type IEnumerator means?
please use more specific titles for your questions in the future.
I have updated the question title so it should be clearer now.
Your answer
 
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How does this script identify other players? C# 1 Answer
Calling a method once in update 1 Answer
Multiple Cars not working 1 Answer
Input versus Event 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                