Coroutines memory allocation on every yield (bug?)

Coroutines are WAAAAAAAY massive overkill here.

If you just need to shake the camera briefly, just make a float timer and count it down.

As long as the timer is nonzero, shake.

This lets you even use the timer as a relative magnitude: you could shake harder at the beginning and have it taper off, or use an AnimationCurve and have it onset / disappear with whatever rate you want.

ALSO: if the Rigidbody above is on this thing, you’re violating physics by manipulating the transform directly. This might end up in you shaking yourself right into the floor or the wall.

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

Coroutines are NOT always an appropriate solution: know when to use them!

I would include my link to Bunny’s coro course but Bunny already did that above!

2 Likes