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 Ego65 · Nov 28, 2015 at 05:25 PM · yieldrotation axisyield waitforsecondsy axis

how to switch between 2 values after 5 seconds using waitforseconds

hi all, i'm lost in yield waitforseconds :( i try to change the y rotation from +90 to -90 every 5 seconds using an IEnumerator when a boolean (is_attacked) = true. dont know if the IEnum.. doesnt work or the way how to set and get the values then in fixedupdate. hope so much someone will help me to get through. P.S: this yield thing is soooo complicated for beginners.
this is my script:

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
 
     public float thrust;
     public Rigidbody rb;
     public float yRotation = 0.0F;
     public bool is_attacked = false;
     public bool turned_left = false;
     public bool turned_right = false;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
 
     }
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(5);
     }
 
     void Update()
     {
         if (yRotation >= 360.0F)
         {
             yRotation = 0.0F;
         }
         if (yRotation <= -360.0F)
         {
             yRotation = 0.0F;
         }
         // yRotation += Input.GetAxis("Horizontal");      
     }
 
     void FixedUpdate()
     {
         y_rotation();
         transform.eulerAngles = new Vector3(0, yRotation, 0);
         rb.AddRelativeForce(Vector3.forward * thrust);
         rb.velocity = transform.forward * rb.velocity.magnitude;
     }
 
     IEnumerator y_rotation()
     {
         if (is_attacked == true)
         {         
             yRotation = -90.0f;
             yield return StartCoroutine(Wait());
             yRotation = 90.0f;
         }
     }
 }
 
 

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by pekalicious · Nov 28, 2015 at 10:51 PM

 y_rotation();

Should be

 StartCoroutine(y_rotation());

Just like you do for Wait(); Keep in mind that when you are not in a coroutine (basically a method that returns IEnumerator) you cannot yield. So you simply call StartCoroutine. If you are in a coroutine, you can either call 'StartCoroutine', or 'yield return StartCoroutine'. The difference is that the latter will wait for that coroutine to complete before continuing, while the former will not.

There are a lot more to it than this, so you might want to check out some tutorials by googling.

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
avatar image
0

Answer by Ego65 · Nov 29, 2015 at 05:45 AM

many thx peka ! i've tried that but now i've got a strange behavior. seems that coroutine is executed as it should only for one (the very first) duration. after that it becomes a mess.
it behaves this way :

  • when is_attacked has become true, yrotation = -90. The ships turns left and moves in the new direction as expected

  • after 5 sec yrotation = +90 as expected but now the ship doesnt react anymore and doesnt change it's direction

  • Then it seems the 5 sec interval becomes a mess and the yrotation is switched in random time intervals(sometimes immediately or in 1 sec or sometimes in more than 5 sec as well as the ship doesn't react.

  • crazy :( so i'v tried to set it in a while loop but this didn't help

      IEnumerator y_rotation()
         {
             while (is_attacked == true)
             {         
                 yRotation = -90.0f;
                 yield return StartCoroutine(Wait());
                 yRotation = 90.0f;
                 yield return StartCoroutine(Wait());
             }
         }
    
      
    
    
    
     
      
    
    
    
    
    
    
    
    
    
    
    
    
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 Ego65 · Nov 29, 2015 at 06:06 AM 0
Share

sure i tried to read tuts and in unity answers help topics for that but to read and understand there, is one thing. But to read and understand and adapt it to the own needing is very much more difficult, as well i'm german my (school english isn't that bad but in such special terms it makes it double difficult for me. so hopefully you'll be patientely with such a poor ol' german beginner in game progra$$anonymous$$g with unity, as i am :)

many thx previously

yours

avatar image pekalicious · Nov 29, 2015 at 04:52 PM 1
Share

That's probably because you are starting the coroutine every single physics update, so you are likely running thousands of coroutines at the same time. Frankly, I'm surprised your machine hasn't exploded.

If I'm understanding correctly, what you want to do is just have your game object rotate every time it's in attack mode, whenever that happens, otherwise don't do anything.

A quick and dirty alternative would be to have a single coroutine execute forever, and when it detects you are in attack mode, then execute your rotation logic.

 IEnumerator y_rotation()
  {
      while (true) // run this forever and ever
      {
          if (is_attacked == true) // if in attack mode, do rotation logic
          {         
              yRotation = -90.0f;
              yield return StartCoroutine(Wait());
              yRotation = 90.0f;
              yield return StartCoroutine(Wait());
          }
      yield return null; // wait a single frame to let Unity breath
     }
  }

Then, ins$$anonymous$$d of calling this coroutine in FixedUpdate, call it once in Start.

avatar image Ego65 pekalicious · Nov 30, 2015 at 04:13 PM 0
Share

that has been basically exactly that what i wanted to achiefe.
thank you very much !

Frankly, I'm surprised your machine hasn't exploded

me too, thats why i like my machine :)

now i have to figure out how to lerp the yrotation as it did with Input.GetAxis("Horizontal") that the moving of the enemy is more realistic

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

36 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

Related Questions

yeild return new WaitForSeconds doesn't work 1 Answer

Question to WaitForEndOfFrame 2 Answers

Invoke method is not called 2 Answers

WaitForSeconds clarification 1 Answer

How do I write yield WaitForSeconds(1.0); from Javascript in C# 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