Hi Guys !
I’m trying to make a 2D pixel game on unity and i’ve some issues.
I use a parent where i put some sprite to make my object and try to move the parent with simple code :
transform.position += Vector3.right * speed;
I use a speed set to be pixel perfect, and my sprites are pixel perfect to. I also use ProCamera2D from the asset store which set my camera size to 1.8 automatically thanks to his plugin Pixel Perfect and I use 100 pixel by unit for my sprites.
And it work well, but at some point, when the x position start to be high (like 1000), the sprites start to shaking weirdly, like juste a little but it’s really annoying. And more the x position grow, more it became shaky. I also notice it stop shaking if i increase the speed, like a lot, but it don’t work for my gameplay.
Any Idea ?
Thank to you
That sounds like a floating point error, but floating point errors shouldn’t start that low. They usually begin to become noticeable around 10,000 from what I’ve seen. It occurs as a result of floats rounding when they are high values.
I’ve used the PixelPerfect system - so i know they change the object position to be ‘pixel friendly’ - which is just some math that figures out how many units to assign per-pixel of your sprite, and then places your sprite at an increment of that. I’m guessing that with a pixels-per-unit of 100 and assuming you’re using a Power-of-Two texture/sprite size, you could ending up with some very long floats for the pixel-perfect re-position, and then the floating point error makes it obvious by shaking around your sprites.
Try changing your Pixels-Per-Units to a power of 2 (4,8,16,32,etc) and see if that helps. I think that will result in fewer decimal places, thus less likelyhood of a floating point error.
Thank you so much ! It work quite well until around 60 000, but it will be quite enough I think, I hope unity will correct it one day. I have to use 128px/unit (i try 4 but the sprite was to big and it quickly was at 60 000 (like in 10 minute but it’s possible for my game)) wich is not very convinient to place the sprite and collider ( one pixel = 0.0078125 unit u_u) but with snapping it’s would be ok I hope (I also multiply my sprite size in photoshop to get the real size in game, so actually one pixel is in fact equal to three to get a non pixel perfect but a more fluid scrolling and parallax).
Thanks again 
60,000 is quite into floating-point-error territory. If you’re going to be needing a large/infinite world, I’d suggest an origin resetter. It basically offsets everything in the world back to 0,0,0 once you reach a certain threshold. You’'ll of course need to setup your game to work with this though - so that means no hardcoded positions unless they’re also offset.
There’s one on the community wiki: http://wiki.unity3d.com/index.php?title=Floating_Origin
Unity won’t really be able to ‘fix’ this - it’s just a fundamental flaw of fixed length values. (ie: floats hold 32 bits) When your number gets too large, you use more of those digits for the whole number, which means less are available for the decimal - so the values are more likely to get rounded. As a result, your object will jitter around as the system rounds its position.
This can also occur on smaller values too - if you’ve ever had a situation when you tried to check if float == float, and it mysteriously doesn’t work: this is why. Floats can change their values.