Hi everyone! I found numerous posts about this problem but no proposed solutions seem to work for me (or for many others who posted). After much digging/debugging, I’ve narrowed it down the following:
while (sqrRemainingDistance > float.Epsilon) // Epsilon is very small number almost zero.
{
Vector3 newPosition = Vector3.MoveTowards(rb2D.position, end, inverseMoveTime * Time.deltaTime);
rb2D.MovePosition(newPosition);
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
yield return null; // Which means waiting for a frame before re-evaluating condition of loop.
}
For some reason it’s getting stuck in this loop. sqrRemainingDistance converges to a really small number but never goes beyond that (e.g. 1.856268E-06 in one test run).
EDIT: 2D Roguelike is a Unity made tutorial. I’m following along the videos and compared my code very closely to source and still don’t see the problem. I even cut and pasted source into my code and it still doesn’t work. When running the source stand alone project however, everything works fine.
Have you printed out the value of float.Epsilon
? Are you familiar with the exponential notation used for floating point numbers to accommodate their wide dynamic value range?
float.Epsilon = 1.401298E-45
Yes, totally familiar with the exponential notation. Just not sure why I’m not seeing sqrRemainingDistance approach epsilon (or zero) … why does it converge to a number other than zero, albeit a small one.
Also, I realize I didn’t make my question totally clear: 2D Roguelike is a Unity made tutorial. I’m following along the videos and compared my code very closely to source and still don’t see the problem. I even cut and pasted source into my code and it still doesn’t work. When running the source stand alone project however, everything works fine.
Seems like an unnecessarily complicated way to move something … maybe it’d be easier to just re-write it?
1 Like
You’re probably right. Maybe I’m more confused than anything … why does it work in one person’s code but not mine?? Alas, moving on!
1 Like
Only the coding Gods know the answers
1 Like
There should be a few of those around here. Hopefully one of them graces us with their presence
1 Like
Solved: under player rigidbody 2D component, I had body type as dynamic instead of kinematic. Changing that solved the problem. Sorry all … newb mistake!!
Don’t think it was your fault, tbh I think it was just bad code that you were given
1 Like