Optimizations: Should I use Update or FixedUpdate? Could you help me optimize rotations?
Hello, I have a lot of MoveTowards and "rotation" type code for my bad guys that are spawned. They rotate (they are balls) and they also MoveTowards a location. How can I optimize this code to run more efficiently? Please also let me know whether MoveTowards is supposed to be in Update() or FixedUpdate... I got confused on where it should be... same with rotation of the object.
This is currently in Update(). Should I be caching the transform or does it matter?
float currentRotation = this.gameObject.transform.localEulerAngles.z;
float newRotation = currentRotation + rotationSpeed;
this.gameObject.transform.localEulerAngles = new Vector3(0f,0f,newRotation);
destination = new Vector2 (randomPositionXStar, randomPositionYStar);
this.transform.position = Vector2.MoveTowards (this.transform.position, destination, speed * Time.deltaTime);
Answer by Salocin19 · Mar 01, 2016 at 04:02 PM
In general, it is recommended to put physics related code (i.e. rotation / move-towards) in fixed update.
The Update() function runs at every frame of the game, whereas FixedUpdate() runs at a fixed rate independent of the framerate.
That being said, you probably don't want your physics to fluctuate if there are spikes of lag, or framerate differences, etc. So, the way to go is most likely FixedUpdate() in your case. In general, it is recommended to put physics related code (i.e. rotation / move-towards) in fixed update.