- Home /
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?
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.
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.
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?
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.
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.
I used while loops before successfully in js, now learning c#. Thx for that, will give it a try!
Your answer
Follow this Question
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