I have this very peculiar bug that when the player is looking a specifik direction (always the same), the _isMoving boolean is set to false even though the player CharacterController is actually still moving.
This is the code:
_isMoving = _cc.velocity.x > 0 || _cc.velocity.z > 0;
This is the only code that actually sets the _isMoving boolean.
I don’t really know what info I could give to help, this is very unexpected and not the type of bug I usually stumble upon (usually only coding errors that aren’t so obvious).
lol it turns out that the camera movement isn’t really affecting anything; it’s just that _cc.velocity.x is less than 0 for the opposite direction (and higher for the other direction) so when I go the axis that have both x and z as less than 0 I don’t count as moving.
I fixed it by using Mathf.Abs on the x and z.
(I’ll leave this maybe someone will benefit from it someday somehow)
Another perhaps more correct solution here would be to check the .magnitude of the vector. This is always a positive value, so you can assume any value over zero (or a very small value) means an object is moving.
Which is best for performance? I really care about performance in my code, above all else.
I’m not sure about how Mathf.Abs and .magnitude differ in terms of logic and performance.
Performance difference is negligible, and you shouldn’t care about such trivial micro-optimisations as they likely make zero difference to your project overall.
I would say that direction.magnitude > 0 is going to be easier for future you to understand.