- Home /
Why changing eulerangles causes so much FPS drop?
I figured out that this part of the code was causing a lot of FPS drop.
transform.eulerAngles.x = 0;
transform.eulerAngles.z = 0;
It looks so harmless. Why does that happen?
Are you sure that this is causing the fps drop? Quaternions might be faster, try that?
It might be the transform access, if you're actually doing this hundreds or thousands of times per frame. Try caching the Transform in a member variable, ins$$anonymous$$d of looking it up every time you need it.
Also, don't set the euler angles' x and z components separately. Under the hood, Unity will transform euler angles to quaternions, and it will have to do the required calculations twice, if you set them this way.
Both of these things should really only have a noticeable impact if you're manipulating a large amount of objects at once (but then the impact can be surprisingly large). Is that the case?
Luckely i dont need those lines of code anymore. I did that because i set the rigidbody to freeze rotation but the eulerangles was slightly changing on the inspector
There is indeed various functions used from $$anonymous$$athf running slower than from System.$$anonymous$$ath. Here I made a replacement example for Vector3.Angle() and Vector2.Angle():
public static float Angle2d(Vector2 a, Vector2 b){
double ax = (double)a.x;
double ay = (double)a.y;
double bx = (double)b.x;
double by = (double)b.y;
double dotd = ax*bx + ay*by;
double a$$anonymous$$ag = System.$$anonymous$$ath.Sqrt(ax*ax + ay*ay);
double b$$anonymous$$ag = System.$$anonymous$$ath.Sqrt(bx*bx + by*by);
double aCos = System.$$anonymous$$ath.Acos(dotd / (a$$anonymous$$ag * b$$anonymous$$ag));
return (float)(aCos*180/3.14159265359);
}
public static float Angle3d(Vector3 a, Vector3 b){
double ax = (double)a.x;
double ay = (double)a.y;
double az = (double)a.z;
double bx = (double)b.x;
double by = (double)b.y;
double bz = (double)b.z;
double dotd = ax*bx + ay*by + az*bz;
double a$$anonymous$$ag = System.$$anonymous$$ath.Sqrt(ax*ax + ay*ay + az*az);
double b$$anonymous$$ag = System.$$anonymous$$ath.Sqrt(bx*bx + by*by + bz*bz);
double aCos = System.$$anonymous$$ath.Acos(dotd / (a$$anonymous$$ag * b$$anonymous$$ag));
return (float)(aCos*180/3.14159265359);
}
performance is very clear when running many thousands calculations on every Update(). I am looking forward to find how eulerAngles is really working (i.e. what $$anonymous$$ath functions can be called) and what would be manual way to replace it.
Your answer
Follow this Question
Related Questions
Why CPU waits for GPU when there is not much work on GPU? 0 Answers
I have some code for the zombie AI but.... 2 Answers
My gun wont fire ! 1 Answer
Any good FPS zombie game names? 1 Answer