Search Unity

Why is this object appear behind in the camera preview?

Discussion in 'General Graphics' started by Fuzzypup, Oct 20, 2020.

  1. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    Working on my 3rd game now selling with a publisher.

    Doing some testing with procedural map creation with an angled map to give a feel like a board game. Meshes and maps work fine and stack properly. When I try and put a 2d object on the screen I get the following. No matter if I put my camera in perspective or orthographic I get the same result. As you can see the object is on top of the hex map behind it. On the camera it shows it behind in the Camera preview. I made a build same problem. I even tried placing the +1.5 world space above the back mesh of hexes. I had to put it +2 for it to appear above it and then it was completely out of position.

    I spent a good 90m searching the internet for solutions and couldn't find any that worked.

    upload_2020-10-20_16-42-54.png
     
  2. Fuzzypup

    Fuzzypup

    Joined:
    Aug 13, 2013
    Posts:
    190
    No one could answer this? I have to the object 2 full world spaces up for it NOT to show like this. Then it is completely distorted on the play map.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Unity's tilemaps are part of it's 2D rendering system, as are sprites and UI. The expectation with those objects is that they generally are going to be manually sorted by you, either via the hierarchy order, or more explicitly by setting the sorting order on the renderer component. Otherwise they're going to be reliant on Unity's 3D sorting system, which uses the spherical bounds of an object to sort. For a tile map I'm guessing the entire tilemap is treated as a single giant object to be sorted against, and large objects are difficult to sort. Again, it's treating it as a spherical bounds, ie: sorting by taking a sphere that covers the entirety of an object and using the distance from the camera to the sphere to determine order. For very large transparent objects, that's going to mean they'll tend to draw on top of smaller objects.

    Sprites are a little different as I think they just sort by their center, ignoring the bounds.

    Either way, the fix is to go to your 2D object's renderer component and increase the sorting order by 1.