• 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 Kensei · Jul 14, 2014 at 05:29 PM · c#freezecompilerwhile

Unity hangs when compiling this.

 private void Timer()
     {
         float countTime = 10f;
         float timeLeft = 0f;
         Vector2 speed = new Vector2(150,150);
 
         while (myRigid.velocity.x < speed.x && myRigid.velocity.y < speed.y)
         {
                 
                 countTime -= Time.deltaTime;
         }
 
 
 
 
         if (countTime <= timeLeft)
         {
             timeUp = true;
         }
         Debug.Log (countTime);
     }

Could anyone tell me how to edit this code so that Unity doesn't freeze? It's basically a timer that is started whenever an object is below a speed threshold.

As a side question, I'd be happy if you could also tell me of a way to compare rigidbody2D.velocity to a Vector2 (x,y);

Comment
Add comment
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

1 Reply

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

Answer by Huacanacha · Jul 14, 2014 at 05:45 PM

Your while loop will never terminate as myRigid.velocity and speed don't change between each loop, therefore once it starts you have an infinite loop. Just use an "if" statement in the Update or FixedUpdate function if you want to count the time the object is below a certain speed.

Also, you are only checking the speed in a single direction. You could be travelling fast in -X direction and still trigger your speed check. I'll assume you want to check speed (directionless) versus velocity (directed), in which case use something like this:

 const float lowSpeed = 200;
 float timeLeft = 0;
 bool timeUp = true;
 
 void StartTiming() {
     timeLeft = 10f;
     timeUp = false;
 }
 
 void FixedUpdate() {
     if (myRigid.velocity.magnitude < lowSpeed) {
         timeLeft -= Time.deltaTime;
     }
 
     if (timeLeft <= 0) {
         timeUp = true;
     }
 }
     
Comment
Add comment · Show 4 · 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 Kensei · Jul 14, 2014 at 06:44 PM 0
Share

Thank you.

avatar image steakpinball · Jul 14, 2014 at 09:29 PM 0
Share

If all you are doing is comparing the magnitude, it is faster to use the sqr$$anonymous$$agnitude property and compare a squared value.

 const float sqrLowSpeed = lowSpeed * lowSpeed;

 if (myRigid.velocity.sqr$$anonymous$$agnitude < sqrLowSpeed) { }

avatar image Kensei · Jul 14, 2014 at 09:33 PM 0
Share

Could you explain why would this be faster? Seems like an additional calculation to me, since the magnitude is a sum of the squares.

avatar image Huacanacha · Jul 14, 2014 at 11:32 PM 1
Share

@brianturner is correct. Using squared speed is faster because you avoid a square root calculation when deter$$anonymous$$ing the Vectors magnitude:

magnitude = $$anonymous$$ath.sqrt(x*x + y*y + z*z)

sqr$$anonymous$$agnitude = x*x + y*y + z*z

Square roots are relatively costly in primitive calculation terms, for example when compared to addition and multiplication. Also note that sqrLowSpeed is calculated once (and is a fast operation anyway) whereas the magnitude is calculated in each FixedUpdate call, hence it is faster to use the squared form.

However: I don't recommend using sqr$$anonymous$$agnitude unless the script will be applied to many many game objects, or if the magnitude call occurs within a loop and is called multiple times, because:

a) Using the simple "speed" keeps code a little clearer, and

b) You won't see any performance difference if magnitude is called once per FixedUpdate

There are so many other costly operations being performed that a few square roots will be lost in the noise, and for the most part you should be coding for clarity rather than performance, then profiling and optimising the code that actually needs it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Unity freezes on play 2 Answers

Distribute terrain in zones 3 Answers

How to solve while loop lags 1 Answer

A node in a childnode? 1 Answer

Multiple Cars not working 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