Efficiency Tips

I’m loving the instruction so far and am excited to complete the rest of the course! Thank you for your enthusiasm and ease of teaching this stuff.

A couple suggestions on how to make the animation and code a little cleaner:

  • You don’t need four transitions if you simply check the absolute value of the move vector’s magnitude. You can check if it’s greater/less than a small value to transition from the idle/run animation with one variable instead of four that way.
  • Maybe this comes later, but should probably normalize the move vector or diagonal movement would be faster than in a cardinal direction.
  • Since the flip test is making a boolean check and the response is setting another boolean value, you can clean that up by simply setting the value to the test. That is: mySpriteRenderer.flipX = mousePos.x < playerScreenPoint.x
2 Likes

My bad! Unity’s (“new”) Input System default mode for the Vector2 is Digital Normalized so no need to normalize the incoming vector!

1 Like

Just wanted to throw my two cents in here. While checking the absolute value of the move vector’s magnitude may be more efficient as far as ease of implementation goes, it is not more efficient from a performance standpoint. Calculating the magnitude every frame takes longer to process than simply checking the properties of the vector.

1 Like

Ah, thank you Tyler! Making things more performance is always a nice lesson, indeed!

Yeah, magnitude is is slower. Especially if you get the absolute value - which is completely pointless. But even calling it 10 million times per frame still only takes 3 milliseconds (versus 2 milliseconds to read the properties 10 million times per frame)

Privacy & Terms