• 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 Tunahanakargul · Jan 20 at 06:04 PM · rotate objectrotating

Coroutine not starting from Update

hello, I wrote a script around me that will return to it when a character appears in a random place, but the problem was that if I run this function on the button, it works, but if I write it in the update method, the code does not work at all, even the working button starts to stop working, but if I remove it from the update method again, the button is the same. it runs the function, but somehow I couldn't run it in the update method code:

 public void startRotating ()
 {
     if (LookCoroutine != null)
     {
         StopCoroutine(LookCoroutine);
     }
     LookCoroutine = StartCoroutine(LookAt());
 }

 public IEnumerator LookAt ()
 {
     Quaternion lookRotation = Quaternion.LookRotation(target.position - transform.position);
     float time = 0;
     while (time < 1)
     {
         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, time);
         time += Time.deltaTime * speed;
         yield return null;
     }
 }

 void OnGUI ()
 {
     if (GUI.Button(new Rect(10, 10, 200, 30), "LookAt"))
     {
         starting();
     }
 }

this example works for me but if i do something like

 void Update ()
 {
     starting();
 }

this but it doesn't work for me in any way what is the reason for this how can i fix it i would appreciate if you help 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by privatecontractor · Jan 20 at 10:38 PM

Hi, @Tunahanakargul,

Try to check is your Coorutine not running multiple time...

Maybe add:

 bool m_coorutineIsRunning = false;
 
 void Update()
 {
 If (!m_coorutineIsRuning) StartCorutine(LookAt());
 }
 
 public IEnumerator LookAt()
  {
          m_coorutineIsRunning = true;
          // your code
          yield return null;
          m_coorutineIsRunning = false;
  }

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 Bunny83 · Jan 21 at 11:14 AM

Uhm, your "startRotating" method does first stops the current running coroutine before starting a new one each frame. So none of your coroutines will ever really run. They all just run up to their first yield and the next frame, when you call "startRotating" again, you stop the coroutine and start a new one. This does not make much sense from the logical point of view.


Starting and stopping coroutines is kinda expensive and if possible you should avoid that. It's far better to keep a single coroutine running. Though from your code it's hard to tell what this is actually supposed to do or how you want to use it. Calling startRotating in Update just makes no sense at all. Besides that using Slerp this way also doesn't make that much sense. Assuming that the coroutine would actually run through, your current use of slerp would result an an accelerated rotation that gets faster and faster towards the end. So using Slerp this way would not be consistent and also changes with the frame rate. Using RotateTowards usually makes more sense. It rotates with a constant rotational speed towards a target rotation when called every frame.


If you just want this object to look at the target (if a target is given), why don't you just do something like this:

 void Update()
 {
     if (target != null)
     {
         var targetRot = Quaternion.LookRotation(target.position - transform.position);
         transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, speed * Time.deltaTime);
     }
 }

There's no need for a coroutine here if you want this behaviour.

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

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

Related Questions

how to rotate a gameobject in a given direction and stop when it's done 1 Answer

Rotate a gameobject by a specific angle and stop when character doesnt move anymore 0 Answers

How can I rotate an object without moving it up or down? 0 Answers

Delayed rotation when i use transform.rotate 1 Answer

How to reset child object's rotation without changing parent's 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