- Home /
Mathf.Approximately allowing too small values (Ruby's Adventure Unity tutorial)
float vertical = Input.GetAxis("Vertical");
float horizontal = Input.GetAxis("Horizontal");
Vector2 move = new Vector2(horizontal, vertical);
if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
{
lookDirection.Set(move.x, move.y);
lookDirection.Normalize();
}
The if statement should prevent normalized lookDirection from becoming (0/0). My problem is that the normalized lookDirection often still ends up being (0/0) if move.x or move.y is small enough. I fixed it by doing the following change
lookDirection.Set(move.x*20, move.y*20);
which works fine for the most part. What I wanted to ask though, is if this is a valid solution (maybe there is something pretty bad about it, that i just dont notice) and if it could/should be done better.
mathf aproximately is returning true when move.x is not exactly 0? is that posible? it is not just a function comparing if both floats are exactly the same with just an epsilon error? and also, how is multiplying * 20 fixing the error? since the normalize of the vector will be the same with and without the *20 so it should not even be affecting, i am missing something?
Your answer
Follow this Question
Related Questions
Problem with lerping Y axis 0 Answers
Adjusting an angle of a collider2D to a linerenderer that is user-created 0 Answers
how do i keep a gameobject in between the union of 2 or more than 2 circles 0 Answers
Set speed by distance from object 1 Answer
Mathf.Atan changes instantly even with a slow lerped value 1 Answer