Sup, i used transform.localpositions , change coordinates of y. I add 10, but in inspector y is becoming like 10, 00001. Or, y slips 0. How i can fixed this ? ths
public void MoveInto()
{
StartCoroutine("Into");
}
public void MoveInto(bool dd)
{
StopCoroutine("Into");
}
IEnumerator Into()
{
while (true)
{
gameObject1.transform.localPosition += new Vector3(0, 10, 0);
if (gameObject1.transform.localPosition.y == 0)
{
MoveInto(true);
}
yield return null;
}
}
That’s just the nature of floats. Floats are never as precise as int, which is why you use Mathf.Approximately when comparing floats.
Yep, i tried so, but sometimes coordinates of y really fast changing. How i can fix this?
Hm, I may have misread your question. I thought you were asking why y was becoming 10.00001.
Anyway…
Firstly, what if y was positive? If you keep adding 10 to that, it will never be 0.
Also even if y was negative, what if it wasn’t a multiple of 10? Suppose it’s -5. If you add 10 to that, it will never be equal to 0.
Finally, 10 is a lot to increment in a single frame. Multiply that by Time.deltaTime as well.
1 Like
As @BlackPete noted above, line 16 appears to move your localPosition up 10 units every frame. That’s fast. If you had the default camera frustum looking up, the object would be visible for only a fraction of a second and out of the view frustum entirely within less than 2 seconds.
To add more, line 17 probably will never be true: floating point values should never be tested for equality. Either use Mathf.Approximately(), or else (in this case) ask if it becomes greater-than-or-equal to zero to decide to stop.
Instead of calling a function to stop your own self as a coroutine, you can also just “yield break;” at line 19, which makes the code a bit neater, unless you intend to also do more stuff in the MoveInto() function in the future.
1 Like