Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 CrossRiverGames · Aug 27, 2016 at 04:55 PM · freezewhile-loop

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!

Comment
Add comment · Show 2
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 mj321 · Aug 27, 2016 at 04:59 PM 0
Share

Put the whole code in a Coroutine and do a "yield return null;" after each iteration of the loop.

avatar image CrossRiverGames mj321 · Aug 27, 2016 at 05:10 PM 1
Share

It works now! Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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
         }
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 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();}

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

54 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

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


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