Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Float precision issue

Discussion in '2D' started by otaku445, May 29, 2014.

  1. otaku445

    otaku445

    Joined:
    May 29, 2014
    Posts:
    2
    Hi there,

    I'm working on a 2D game project for school. It is in Pokemon-style (topview, tile system).
    And now I've got a problem with the floats of the x and y coordinates of the tiles. When I layed out the tiles the x and y coordinates were just fine (for example x: 0.32, y: 0.64 - I've got 32x32 tiles). But after a few test runs of the game there were gaps between some tiles. The x and y coordinates got more and more changed because of the float precision issue. I've already googled how to set a global (in the whole game) float precision or how to prevent the values from changing but I didnt find anything useful. I also wrote a script that rounds the coordinates (in the start function) and applied it on each tile, but even then the gaps appeared. I also tried to put the function into the Update function, but also there the gaps appeared. And now I totally ran out of ideas how I could fix this.

    Thanks for your help!
    otaku445
     
  2. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256
    Can't help you without seeing the source.
     
  3. otaku445

    otaku445

    Joined:
    May 29, 2014
    Posts:
    2
    The problem isnt some script I just tried to fix the problem with a script. The problem is that the float values for the x and y coordinates change everytime i run the game. So if I have for example 0.32 for x it is 0.3200001 at the second run and 0.3200004 at the fourth and so on. So it moves slighty until you can see gaps.
    Source code for the script with which I tried to solve this:
    Code (csharp):
    1. transform.position = new Vector3(Mathf.Round(transform.position.x * 100.0f) / 100.0f, Mathf.Round(transform.position.y * 100.0f) / 100.0f, transform.position.z);
    otaku445
     
  4. TomasJ

    TomasJ

    Joined:
    Sep 26, 2010
    Posts:
    256

    Could you send me a full repro case? That snipped isn't enough.