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 /
  • Help Room /
avatar image
0
Question by arkea · Jan 28, 2016 at 02:35 PM · vrcoroutinewaitforsecondselevator

Coroutine problem with elevator

Hi everyone.

Can someone resolve my problem ? I ve made an elevator but it doesn't behave as i've expected. there is the code :

 public Animator anim, doorAnim;
 public bool IsEntered;
 public GameObject Player;


 void Start () {
     IsEntered = false;
 }

 void OnTriggerEnter () {
     IsEntered = true;
 }
 void OnTriggerExit () {
     IsEntered = false;
 }
 public void onUpperClick () {
     
     if (IsEntered) {// Have to be in the Elevator to activate the button
         
         Player.GetComponent<CharactControl>().SetCanForward (false);// My Player stop walking
         doorAnim.SetTrigger ("Close"); // Close the Door of the Elevator
         StartCoroutine(wait2seconds()); // Wait that the door is close
         anim.SetTrigger ("Up");//Elevator goes to 1th Floor

     }

 
     StartCoroutine(wait3seconds()); //Wait that the Elevator has climbed
     doorAnim.SetTrigger ("Open");//Open the Door
 }

 public void onDownClick () {

     if (IsEntered) {
         
         Player.GetComponent<CharactControl>().SetCanForward (false);
         doorAnim.SetTrigger ("Close");
     
         StartCoroutine(wait2seconds());
         anim.SetTrigger ("Down");
     }
         
     StartCoroutine(wait3seconds());
     doorAnim.SetTrigger ("Open");
 }

 IEnumerator wait3seconds () {
 
     yield return new WaitForSeconds (3);
 }
 IEnumerator wait2seconds () {

     yield return new WaitForSeconds (2);
 }

} Actually the coroutine works great but doesn't stuck the code below the method calling... :'( I understand the problem but i can't resolve it... Thanks

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
1
Best Answer

Answer by corn · Jan 28, 2016 at 04:01 PM

You misunderstood how Coroutines work. They do not stop the execution flow of the function that calls them, they are executed separately, on a frame-by-frame basis. Now inside a Coroutine you can indeed stop the execution flow for a designated period of time.

In this case you want to delay execution of some code. You just have to write said code inside the Coroutine, after WaitForSeconds.

Coroutines can also have arguments, so be sure to take advantage of that ! There's no need to write several singular-purpose coroutines, while you can just define one that will work for all your use cases :

 IEnumerator DelayedTrigger(Animator anim, string trigger, float delay)
 {
     yield return new WaitForSeconds(delay);
     anim.SetTrigger(trigger);
 }

Then you just have to replace

 doorAnim.SetTrigger ("Close"); // Close the Door of the Elevator
 StartCoroutine(wait2seconds()); // Wait that the door is close
 anim.SetTrigger ("Up");//Elevator goes to 1th Floor

With

 doorAnim.SetTrigger ("Close"); 
 StartCoroutine(DelayedTrigger(doorAnim, "Up", 2f)); 

And do the same for all similar operations.

Comment
Add comment · Show 5 · 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 arkea · Jan 28, 2016 at 06:06 PM 0
Share

thank's man :)

avatar image arkea · Feb 11, 2016 at 10:18 AM 0
Share

Hi man, i'm currently using what you told me :

public class Elevator_Controller : $$anonymous$$onoBehaviour {

 public Animator anim, doorAnim;
 public bool isEntered;
 public GameObject player;
 public bool lastOperation;
 public GameObject buttonSound;
 public GameObject doorSound;


 void Start () {
     
     isEntered = false;
     lastOperation = false;
 }

 void OnTriggerEnter () {

     isEntered = true;
 }

 void OnTriggerExit () {

     isEntered = false;
 }

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: public void onUpperClick () {

     if (isEntered) {        // Have to be in the Elevator to activate the button

         player.GetComponent<CharactControl>().SetCanForward(false);// $$anonymous$$y Player stop walking
         buttonSound.GetComponent<AudioSource>().Play();
         if (!lastOperation) {
             
             StartCoroutine (DelayedTrigger (doorAnim, "Close", 0.6f, doorSound, 0.5f));
             StartCoroutine (DelayedTrigger (anim, "Up", 2.5f));
             StartCoroutine (DelayedTrigger (doorAnim, "Open", 6f, doorSound, 0.05f));
             lastOperation = true;
         }      
     }
     
 }

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: public void onDownClick() {

     if (isEntered) {            // Have to be in the Elevator to activate the button
         
         player.GetComponent<CharactControl>().SetCanForward(false);// $$anonymous$$y Player stop walking
         buttonSound.GetComponent<AudioSource>().Play();
         if (lastOperation) {
             
             StartCoroutine (DelayedTrigger (doorAnim, "Close", 0.6f, doorSound, 0.05f));
             StartCoroutine (DelayedTrigger (anim, "Down", 2.5f));
             StartCoroutine (DelayedTrigger (doorAnim, "Open", 7f, doorSound, 0.2f));
             lastOperation = false;
         }
     }
 }

///:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: IEnumerator DelayedTrigger (Animator anim2, string trigger, float delay, GameObject sound, float offset) {

     yield return new WaitForSeconds(delay);
     anim2.SetTrigger(trigger);
     yield return new WaitForSeconds (offset);
     sound.GetComponent<AudioSource>().Play();

 }

 IEnumerator DelayedTrigger (Animator anim2, string trigger, float delay) {

     yield return new WaitForSeconds(delay);
     anim2.SetTrigger(trigger);
 }

} but it doesn't work so well because it can be good for first time and second (not always) and after can goes worse and worse, why ?

avatar image arkea · Feb 11, 2016 at 01:17 PM 0
Share

hey, i 've also tried something else... Same issue...

public class elevator2 : $$anonymous$$onoBehaviour {

 private float timeToDoorAct = 2.25f;
 private float timeToDoorAct2 = 2.65f;
 private float timeToElevatorUp = 3.0f;
 private float timeToElevatorDown = 2.5f;
 public Animator anim,doorAnim;
 public bool hasLifted;
 public bool isEntered;

 void Start () {

     hasLifted = false;
     anim = GetComponent<Animator>();
 }

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: void LaunchRaising () {

     anim.SetTrigger ("Up");
     Invoke ("LaunchOpening", timeToElevatorUp);
     hasLifted = true;
 }
 void LaunchLanding () {

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: anim.SetTrigger ("Down"); Invoke ("LaunchOpening", timeToElevatorUp); hasLifted = false;

 }

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 void LaunchOpening () {
 
     doorAnim.SetTrigger ("Open");
 }

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 public void OnUpperClick () {

     if (!hasLifted) {
         if (isEntered) {
     
             doorAnim.SetTrigger ("Close");
             Invoke ("LaunchRaising", timeToDoorAct);    
         }
     }
 }

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 public void OnDownClick () {

     if (hasLifted) {
         if (isEntered) {

             doorAnim.SetTrigger ("Close");
             Invoke ("LaunchLanding", timeToDoorAct2);
         }
     }
 }

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 void OnTriggerEnter () {
                             //Quand le Player rentre dans l'ascenseur on peut lancer les interactions
     isEntered = true;
 }

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 void OnTriggerExit () {
                             //Quand le Player sort on stoppe les possibilités d'interactions 
     isEntered = false;
 }

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: }

avatar image corn arkea · Feb 14, 2016 at 10:39 AM 0
Share

@arkea What's the problem exactly ? (Tu peux écrire en français et en anglais si tu n'es pas très à l'aise avec l'anglais)

avatar image arkea corn · Feb 15, 2016 at 07:41 AM 0
Share

En fait, j'ai des décalages qui peuvent surgir de nulle part et du coup les animations prennent plus de temps. C'est trop bizarre en fait,genre par exemple : premier aller retour tout se passe bien, le deuxieme par exemple quand je vais redescendre je vais cliquer sur le boutton "Down" la porte va se fermer et puis peut très bien attendre 2 secondes avant de descendre et du coup faire ouvrir la porte alors que l'ascenseur ne sera pas encore arrivé en bas ><

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

43 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# coroutine not waiting on waitForSeconds? 2 Answers

Could not load source 'Coroutines.cs': No source available. 1 Answer

Am I using this Coroutine and IEnumerator correctly? 1 Answer

Trying to make my code pause using coroutines, what am i doing wrong? 2 Answers

Performance Issue with Coroutines 2 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