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 SaganTucker · Jul 06, 2016 at 07:29 PM · coroutinesevent triggeringcallback

Custom Event System, Coroutines, Callbacks?

I'm trying to call events from various circumstances.

I have a custom EventManager set up from a tutorial. It's working fine; I call the first event when the player enters a Trigger, which causes an NPC to approach him and say something. The problem is I want to call the second event when the player reaches a certain distance from the NPC, but I'm not sure how to go about it. I've tried a coroutine but I'm very new to them so I'm not sure if it's the best way to go about this. I'm also not sure how I would go about adding a function callback so I can trigger the desired event from the coroutine. Can anyone help me?

I'm not sure which code would be helpful, I've added my Events code below, if there's anything else you'd like to see just let me know and I'll add it here.

 public class Events : MonoBehaviour {
 
     private UnityAction event01;
     private UnityAction event02;
 
     public Transform player;
     public CompanionAI Laurelai;
 
     void Awake()
     {
         event01 = new UnityAction(Event01Function);
         event02 = new UnityAction(Event02Function);
     }
 
     void OnEnable()
     {
         EventManager.StartListening("Event01", event01);
     }
 
     void OnDisable()
     {
         EventManager.StopListening("Event01", event01);
     }
 
     IEnumerator WaitForDistance(Vector3 a, Vector3 b, float distanceToCheck) //function callBack?
     {
         while(Vector3.Distance(a, b) < distanceToCheck)
         {
             yield return null;
         }
         Debug.Log("Distance reached");
         //I thought of adding some kind of function callback here so I could call a function to trigger the next event, but I don't know how to go about it.
     }
 
     void Event01Function()
     {
         //Debug.Log("Event01 called");
         Laurelai.followTarget = player;
         Laurelai.LookAtPlayer(true);
         Laurelai.isFollowing = true;
         Laurelai.Talk();
         EventManager.StartListening("Event02", event02);
         StartCoroutine(WaitForDistance(Laurelai.transform.position, player.position, Laurelai.followDistance));
         //EventManager.TriggerEvent("Event02");
     }
 
     void Event02Function()
     {
         Laurelai.Talk();
     }
 }
 

Maybe I'm going about the whole thing the wrong way? Thanks for any help anyone can give me!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SaganTucker · Jul 10, 2016 at 12:06 AM

Found a solution!

Took me a while of experimentation, but the problem was my coroutine. I didn't really understand how they worked. It was checking the same values over and over, derp.

I changed it to check the updated values each time. I've also added a coroutine to check if the audio source has finished playing. Then I added another coroutine to manage the whole Event Scenario. I feel like I've become a lot more comfortable with coroutines!

Here's my new class:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Events;
 
 public class Event01 : MonoBehaviour {
 
     private UnityAction event01;
 
     public Transform player;
     public CompanionAI Laurelai;
 
     void Awake()
     {
         event01 = new UnityAction(Event01Function);
     }
 
     void OnEnable()
     {
         EventManager.StartListening("Event01", event01);
     }
 
     void OnDisable()
     {
         EventManager.StopListening("Event01", event01);
     }
 
     void Event01Function()
     {
         EventManager.StopListening("Event01", event01);
         //Debug.Log("Event01 called");
         StartCoroutine(LaurelaiSequence01());
     }
 
     IEnumerator LaurelaiSequence01()
     {
         Laurelai.followTarget = player;
         Laurelai.LookAtPlayer(true);
         Laurelai.isFollowing = true;
         Laurelai.SayLine(0);
         yield return StartCoroutine(WaitForEndOfAudioClip(Laurelai.audiosource));
         yield return StartCoroutine(WaitForOutOfRange(Laurelai.transform, player, Laurelai.followDistance + 1));
         Laurelai.SayLine(1);
         yield return StartCoroutine(WaitForEndOfAudioClip(Laurelai.audiosource));
         yield return StartCoroutine(WaitForOutOfRange(Laurelai.transform, player, Laurelai.followDistance + 1));
         Laurelai.SayLine(2);
         yield return StartCoroutine(WaitForEndOfAudioClip(Laurelai.audiosource));
         //EventManager.TriggerEvent("Event02");
     }
 
     IEnumerator WaitForEndOfAudioClip(AudioSource audiosource)
     {
         while (audiosource.isPlaying)
         {
             yield return null;
         }
         Debug.Log("Audio Ended");
     }
 
     IEnumerator WaitForOutOfRange(Transform a, Transform b, float range)
     {
         while (Vector3.Distance(a.position, b.position) < range)
         {
             yield return null;
         }
         Debug.Log("Out of range");
     }
 }
 
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity Web Player WWW Call 0 Answers

Waiting for and using output of a UnityEvent within coroutines 1 Answer

Where is the best place to use delegate call back without calling it in Update function? 1 Answer

Assign Event Trigger of object from another object in script 1 Answer

Passing parameters dynamically with Unity Event 0 Answers


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