x


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.

more ▼

asked Jun 09 '11 at 07:52 AM

fisyher84 gravatar image

fisyher84
63 10 10 13

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. ;)

Jun 10 '11 at 02:16 AM Joshua
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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");
}

}

more ▼

answered Jun 09 '11 at 07:59 AM

testure gravatar image

testure
4.2k 20 25 48

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?

Jun 09 '11 at 09:25 AM fisyher84

please use more specific titles for your questions in the future.

Jun 09 '11 at 11:49 AM Anxowtf

I have updated the question title so it should be clearer now.

Jun 10 '11 at 01:50 AM fisyher84
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4368
x3418
x513
x171

asked: Jun 09 '11 at 07:52 AM

Seen: 1268 times

Last Updated: Jun 10 '11 at 02:16 AM