- Home /
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);
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;
}
}
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) { }
Could you explain why would this be faster? Seems like an additional calculation to me, since the magnitude is a sum of the squares.
@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
![](https://koobas.hobune.stream/wayback/20220613154039im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to solve while loop lags 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
Unity freezes on play 2 Answers