Week 3: Code optimization


This week was used to optimize the move/rotate code from last time.  Instead of updating the position frame by frame whether or not it needs to change, I use a series of if statements and boolean flags to check if the transform methods have been called:

These flags are set to true whenever the method is called, and reset after a transform has completed.  I changed object prefabs to all have a Rigidbody 2D component so that I don't have to check for it multiple times in the code.   For now, I check if an object is already kinematic before setting it to kinematic to disable physics while the code is running.  This is to prevent the jittering I experienced last week.



I changed the method to be FixedUpdate instead of Update so it wouldn't compete with the physics system.  Here's the full  FixedUpdate() code:


As you can see in the move and method blocks here, I moved the old transform lines inside of a Rigidbody method, which should work better with the physics system.


Currently, the main issue is if I don't make dynamic objects kinematic before starting this code, they'll stop in midair and won't become dynamic again because the position isn't close enough to snap.  If I do make them kinematic, they'll clip through colliders and platforms unless I code in collision manually.  This'll be something I look at over the next week.



Here's what it looks like currently:




I also finished the first big sweep of the Level 2 tileset. I'll update it as needed when I actually implement it, since I always find more things to add, but this is what I've got as of now:


Note the new light frame, platforms, robotic wreckage, and the large circle-like tunnel.


Some things don't appear and the animation is not smooth because the water is 4 frames and the waterfall is 3, but it should look smooth in-game.


I learned about processing costs, optimization and a lot about how Unity works with its 2D physics versus code this time. Until next week!

Leave a comment

Log in with itch.io to leave a comment.