Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fisyher84 · Jun 09, 2011 at 07:52 AM · c#updateevent

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Jun 10, 2011 at 02:16 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fisyher84 · Jun 09, 2011 at 09:25 AM 0
Share

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?

avatar image Anxo · Jun 09, 2011 at 11:49 AM 0
Share

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

avatar image fisyher84 · Jun 10, 2011 at 01:50 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges