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 /
  • Help Room /
avatar image
0
Question by Mr0w3m · Sep 02, 2015 at 03:13 PM · lerpcrashingcrasheswhile loop

So this code is crashing Unity only sometimes...

So I've narrowed it down to the while loop by commenting it out and testing it. So I have no idea why but some times it works in play mode every time, then I exit play mode, re enter play mode and it freezes up and I have to manually end the application.

         if (BlueSquareClicked == true && Physics.Raycast (camRay, out hit, camRayLength, BlueMoveMask)) 
         {
             Debug.Log ("Clicked on BlueMoveTile");

             startPoint = transform.position;
             endPoint = hit.transform.position;

             BlueSquareMoving = true;


             float journeyLength = Vector3.Distance (startPoint, endPoint);
             float distCovered = (Time.time - startTime) * moveSpeed; 
             float journey = distCovered / journeyLength;

             while (BlueSquareMoving == true) 
             {
                 Debug.Log ("Here");
                 //BlueSquareMoving = false;
                 transform.position = Vector3.Lerp (startPoint, endPoint, journey);
                 //Debug.Log ("Moving");

                 if (transform.position == endPoint) 
                 {
                     Debug.Log ("Finished Moving");

                     BlueSquareMoving = false;
                     BlueSquareDoneMoving = true;
                     BlueSquareClicked = false;

                     GameManager.killTiles = true;
                 }
             }
         }


Any Help would be awesome. If I need to post the entire script let me know.

Comment
Add comment · Show 4
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 Scribe · Sep 02, 2015 at 03:43 PM 0
Share

likely to be floating point errors meaning that transform.position never exactly equals endPoint. You could make that an approximate check, i.e. if the distance between them is less than something very small, finish.

Alternatively you could check when (journey >= 1) and it might be more successful

avatar image Mr0w3m · Sep 02, 2015 at 04:11 PM 0
Share

@Scribe Idk I'm new to the way this website works... I think this will go in the right place...

The only problem with accepting and stopping before that exact number is that when the piece moves again it will be slightly off course and eventually will be out of line? The game works similar to chess, I'm still ironing out the turnbased movement system.

avatar image Scribe · Sep 02, 2015 at 04:30 PM 0
Share

You can avoid that problem by calculating your endPosition based off of the central position of the square the piece should be in, rather than by calculating it from an offset of your start position. It looks like this is what you already do, so you should have no issues with it being slightly offset from the actual position.

Though I doubt you would notice any issues doing calculations the other way with any normal length chess game (you probably won't see a difference of 1E-7)!

avatar image Mr0w3m · Sep 03, 2015 at 03:28 PM 0
Share

You were correct first of course but I didn't quite get what you were saying until @sharat replaced the loop function with an if function and what you had suggested. Thanks for the help!

1 Reply

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

Answer by sharat · Sep 02, 2015 at 07:46 PM

Shouldn't while (BlueSquareMoving == true) be an if statement? There are two cases for what's happening in the loop. Either journey >= 1 and it's run exactly once since BlueSquareMoving gets set to false. Or journey < 1 and it will keep on setting transform.position to the same value over and over in an infinite loop(causing your freeze). I'd replace that loop with:

          if(BlueSquareMoving == true) 
          {
              transform.position = Vector3.Lerp (startPoint, endPoint, journey);
              if (journey >= 1.0f) 
              {
                  BlueSquareMoving = false;
                  BlueSquareDoneMoving = true;
                  BlueSquareClicked = false;
                  GameManager.killTiles = true;
              }
          }
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

29 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

Related Questions

Timeline causes IOS app to crash when adding it, how do i fix this/ 0 Answers

Application.TickGlobalCallbacks causing crashing! 0 Answers

Android App Crashes Randomly 0 Answers

My game crashes with the following logs,Please help me out. 0 Answers

"If statement" and whiled loop acting strange and crashes 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