- Home /
While loop causes game to freeze
I have a while loop that is intended to trigger when the player character (which the code in question is attached to) moves out of view of the main camera and move the camera in the proper direction until the player character is once again in view of the camera:
private void MoveCamera()
{
Vector3 lowerLeft = playerCamera.ViewportToWorldPoint(new Vector3(0, 0));
Vector3 upperRight = playerCamera.ViewportToWorldPoint (new Vector3(1, 1));
bool left;
bool up;
bool right;
bool down;
while (!r.isVisible) {
Debug.Log ("Still off the screen");
// Determine what sides of the screen the player is off
if (transform.position.x <= lowerLeft.x) {
left = true;
} else {
left = false;
}
if (transform.position.y <= lowerLeft.y) {
down = true;
} else {
down = false;
}
if (transform.position.x >= upperRight.x) {
right = true;
} else {
right = false;
}
if (transform.position.y >= upperRight.y) {
up = true;
} else {
up = false;
}
// Determine where to move the camera
if (up && right) {
cameraEndPoint.Set (cameraStartPoint.x + 0.1f, cameraStartPoint.y + 0.1f, cameraStartPoint.z);
}
else if (down && right) {
cameraEndPoint.Set (cameraStartPoint.x + 0.1f, cameraStartPoint.y - 0.1f, cameraStartPoint.z);
}
else if (down && left) {
cameraEndPoint.Set (cameraStartPoint.x - 0.1f, cameraStartPoint.y - 0.1f, cameraStartPoint.z);
}
else if (up && left) {
cameraEndPoint.Set (cameraStartPoint.x - 0.1f, cameraStartPoint.y + 0.1f, cameraStartPoint.z);
}
else if (up) {
cameraEndPoint.Set (cameraStartPoint.x, cameraStartPoint.y + 0.1f, cameraStartPoint.z);
}
else if (right) {
cameraEndPoint.Set (cameraStartPoint.x + 0.1f, cameraStartPoint.y, cameraStartPoint.z);
}
else if (down) {
cameraEndPoint.Set (cameraStartPoint.x, cameraStartPoint.y - 0.1f, cameraStartPoint.z);
}
else if (left) {
cameraEndPoint.Set (cameraStartPoint.x - 0.1f, cameraStartPoint.y, cameraStartPoint.z);
}
// Actually move the camera
playerCamera.transform.position = Vector3.Lerp(cameraStartPoint, cameraEndPoint, (Time.time - startTime) / duration);
// Reset camera start point
cameraStartPoint.Set(cameraEndPoint.x, cameraEndPoint.y, cameraEndPoint.z);
}
When I change the while loop to an if statement, the code executes properly, but only once. I want the code to execute until the player character is once again in view of the camera; what am I doing wrong? Thanks!
Put the whole code in a Coroutine and do a "yield return null;" after each iteration of the loop.
Answer by Aldeminor · Aug 27, 2016 at 05:34 PM
Maybe you should make things work a bit different?
if (!r.isVisible)
{
//Do yor stuff about detecting where the player
}
while (!r.isVisible)
{
//Move the camera before player appears again
}
Answer by WAELK · Aug 28, 2016 at 12:21 AM
keep it as an if statement and place your iterations in void Update (){} ;
I'd say void Update(){ MoveCamera();}
Your answer
Follow this Question
Related Questions
beginner coder question, is this code right? 1 Answer
Unity Crashes From While Loop and Can't fix 0 Answers
Do-while loop freezes the engine when running the game -1 Answers
What is wrong in my code - Unity freezes 1 Answer
How to use a WHILE LOOP? Obviously not inside ***function Update*** 3 Answers