Tile tearing in Unity solution finally found!

Something that has been bothering us ever since Unity developed its 2D engine. We know we are not alone when it comes to 2D development and line tears showing between tiles. What a frustrating issue that seems to be a Unity related issue. We tried everything we could think of, and while many solutions reduced tearing, it didn’t get rid of it.

line issue

The solutions we tried include:
1. Set pixels to equal unity units in a 1:1 ratio. Default is 100 to 1. Was one of the best solutions, and made tile placement much easier.
2. Tried creating a default shader that allowed sprites to “snap to pixel.” This did not work for us, nor show any improvement.
3. We are using no compression-highest quality sprites we can. Our compression level is 8192, with format set to Truecolor, turned off mip-maps, and point filtering mode.
4. Anti-aliasing is turned off, but didn’t seem to help all that much.
5. While overlapping tiles does indeed fix the issue, the problem is that you lose tile snapping and have to adjust the 1 pixel border manually when placing new tiles. While this could work, it also means a LOT more time spent creating levels.
6. We confirmed our orthagraphic camera size so that our tiles would not be scaling at all. For us this was 360 based on a tile of 32×32.
7. Made sure that our tiles are being placed in integer unity-global coordinates. Didn’t seem to make much difference.
8. Confirmed all sprite tiles causing issue were power of two. We knew that they were 32×32 but wanted to verify if it was something stupid.
9. Finally we bounded the camera to integer usage only.

This was a really big pain for us. I spent months researching online, but could only find other developers suffering from the same issue. I really did not want to implement the overlap method because it wasn’t really a fix, and would cause other headaches to deal with. Finally, I found a blog post explaining that the developer bounded the camera to integers to remove its movement in floats. I didn’t know exactly how to do that, nor could I find many articles that could explain it well. I finally found the code snippet I needed, which I am going to share with you now.

line issue code

In case it isn’t showing up well for you, the code is as follows:
gameObject.transform.position = new Vector3((Mathf.RoundToInt(gameObject.transform.position.x)),
(Mathf.RoundToInt(gameObject.transform.position.y)),
(Mathf.RoundToInt(gameObject.transform.position.z)));     //used to keep camera bounded to integers

As you can see the same spot no longer shows us a line, as well as our gameplay has so far verified that the solution is fixed. This is a huge triumph for us. I personally felt really stuck by this issue because I know that I can’t release the “perfect” game, but I want it to simulate a high amount of quality. And such an issue really, really irritated me. Especially because so many people were suffering from it, and no one had posted this solution. So, now doing our community a favor, here is our solution. Thank you for reading this, and I hope it helped you.

line issue gone

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *