It’s said that premature optimization is the root of evil in programming. I’d like to recount a 4 hour long waste of time that I caused because of my own premature optimization.
When I started this project, a strictly 2d game, I started by putting thousands of sprites on the screen and looking at the profiler. I noticed most of my time was spent in a Physics update routine. I decided early on to do physics myself because this game wasn’t going to use anything more complex than pretty simple translation. Translation would be moving an object from one point to another over time. I found out you can reduce the times that the game engine recalculates its physics by changing something called the TimeStep. I cranked that sucker to maximum and went on my merry way.
4 months later it came back to bite me. I thought I didn’t use anything from the Unity Phyiscs library, but it turns out I was wrong. The box colliders that surround the buttons in my game use a RayCast to see if the mouse or your finger is over them. All has been well until a few days ago when I started animating the menus. They now slide on and off the screen. It looks pretty sweet, but the weirdest thing would happen. After moving a menu on the screen, the buttons would be unresponsive for around 10 seconds.
After several hours of tweaking and debugging I tracked it down to a RayCast failing to hit an obviously good condition. In the screenshot above you can see the mouse pointer is well within the green box collider of the button; however, the physics engine would report a failure when raycasting.
The issue turned out to be the TimeStep variable above which was set to maximum. Guess what that value is at maximum? 10 seconds.. It makes sense that the physics engine couldn’t update the location of the box colliders until it ran again, which I naively set to maximum those many months ago. Fortunately I am reminded once again to stop premature optimization.