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 Ambrose998800 · Oct 05, 2016 at 11:07 AM · loopwhile

While loop crashing

Hi

Once more someone (me) has problems with a while loop. I understand logic, but this one is still a riddle to me...

Code:

 Angle = 180;
 while (!Mathf.Approximately(Angle, 100))
 {
      Angle = Mathf.LerpAngle(180, 100, Time.deltaTime);
      DoorVector = new Vector3(transform.localEulerAngles.x, Angle, transform.localEulerAngles.z);
      transform.localEulerAngles = DoorVector;
 }

So, while loop will be running while Angle (starts with value 180) isn't close to 100, and when while loop is running, Angle will drop down to value 100 in 1sec. That means, after 1sec Angle should be appr. 100 and the loop should end, isn't it?

Why does it crash/freeze?

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 doublemax · Oct 05, 2016 at 11:14 AM 1
Share
 Angle = $$anonymous$$athf.LerpAngle(180, 100, Time.deltaTime);


All three values passed to LerpAngle are constant, so Angle will always be the same and never reach 100.

Try passing Angle ins$$anonymous$$d of 180.

You also need a yield call inside the loop.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ByteSheep · Oct 05, 2016 at 11:30 AM

The problem is that you're trying to do an entire gradual process within just one frame. You should move your code to the update function or use a coroutine, e.g.:

 private IEnumerator LerpDoorAngle(float duration = 1f)
 {
     float angle = 180f;
     float startTime = Time.time;
     while (Time.time - startTime <= duration)
     {
         angle = Mathf.LerpAngle(180, 100, (Time.time - startTime) / duration);
         var doorVector = new Vector3(transform.localEulerAngles.x, angle, transform.localEulerAngles.z);
         transform.localEulerAngles = doorVector;
         yield return null;
     }
 }
 
 // And then call the coroutine like this:
 StartCoroutine(LerpDoorAngle(1f));

The problem with your original loop is that it doesn't wait until the next frame before checking Time.deltaTime again.

Comment
Add comment · Show 2 · 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 Ambrose998800 · Oct 05, 2016 at 04:39 PM 0
Share

The while loop should extend the process to more then one frame, I used it before in js that way. I don't want to use the Update() funtion cause this script is running on some hundred objects simultaneously.

Where should the StartCoroutine() be placed in the script?

avatar image ByteSheep Ambrose998800 · Oct 05, 2016 at 04:52 PM 0
Share

The StartCoroutine() call should only be called once, not every frame (for example in Start() or Awake()). You could also call it once in the Update() method as soon as some condition is met and you want your door to start rotating. This is similar to the way you probably did it in JS, except that in C# we have to explicitly define the return type to be an IEnumerator if we want to yield return within a while loop.

avatar image
1

Answer by LK84 · Oct 05, 2016 at 11:22 AM

I don't see where you are decreasing your value for Angle.The statement Angle = Mathf.LerpAngle(180, 100, Time.deltaTime);always returns a value between 180 and 100. Time.deltaTime returns the time in seconds it took to complete the last frame, so if you have a FPS of 30 Time.deltaTime=1/40, the resulting angle always will be 180-(180-100)/40=178 degree. So you need to add some time dependency to decrease the value.

Comment
Add comment · Show 1 · 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 Ambrose998800 · Oct 05, 2016 at 03:22 PM 0
Share

I used while loops before successfully in js, now learning c#. Thx for that, will give it a try!

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

58 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

Related Questions

Using Loops 1 Answer

What part of my While Loop is causing the game to freeze? (C#) 2 Answers

StopCoroutine() is not stopping my coroutines 1 Answer

Add 2 Parameters in While loop 3 Answers

Breaking from a Loop 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