0

I am trying to move an object 0.32 units in Unity, and I need it to be exactly 0.32 to work with the rest of the system. When I run it, it seems fine, but when I look at all the instances of what I am placing some of them are x = 0.64, y = 0.959999999 etc... it breaks my system pretty much every time I run it. Is there any way I can be for certain that it will move the number of units I give it?

private void Move()
    {
        int i = Choose.Pick(4);
        int dir = i;
        if (dir == 0)
        {
            transform.Translate(new Vector3(0.32f, 0, 0));
        }
        else if (dir == 1)
        {
            //transform.position += Vector3.down * 0.32f;
            transform.Translate(new Vector3(0, -0.32f, 0));
        }
        else if (dir == 2)
        {
            //transform.position += Vector3.left * 0.32f;
            transform.Translate(new Vector3(-0.32f, 0, 0));
        }
        else
        {
            //transform.position += Vector3.up * 0.32f;
            transform.Translate(new Vector3(0, 0.32f, 0));
        }

        PlaceFloor(transform.position);
    }
Ben Lusted
  • 13
  • 4
  • That’s just how limited-precision maths works. If you need exact values, use integers. But your system simply shouldn’t break in the presence of small errors: there’s fundamentally no way around them. – Konrad Rudolph Aug 02 '21 at 21:56
  • @KonradRudolph Can you add [this](https://stackoverflow.com/q/64036583/1092820) as another (more specific to unity) duplicate – Ruzihm Aug 02 '21 at 21:58

0 Answers0