unity-game-engine2dgame-developmenttopdownurp

2D layering/URP issue when using fence tiles


Character runs into fence and appears correctly above fence

Character runs into fence and incorrectly appears above fenceURP to show that axis is (0, 1, 0)

I am working on my first real 2D unity game. I only recently started using URP to display my character behind barrels and cacti correctly. I am now using a fence tile set I created with collider boxes along the bottom edges of the fence. However, when the player's y-value is greater than that of the fence, the player incorrectly appears above the fence (It does appear correctly when the player's y-value is less than the fence's.

I think it has something to do with pivots, however when I try to adjust pivots, sprites do not perform as intended. Also, they are located on the same layer.

Thank you in advance for any help. Please ask if you need any clarification.

EDIT: Most of the fence tiles are two tiles in height. I switched the collider type to None for the tops of fences so that they wouldn't collide with the player. Could this be a problem?

Camera Component


Solution

  • You need to change a number of configurations to resolve your issue.

    1. Remove the Sort Group Component from the "Fences" gameobject. It is intended for something different and messes with sorting.
    2. Change the Mode of the Tilemap Render on the "Fences" gameobject, so that it is set to Individual. This is the intended mode for interweaving transparent tilemaps and other sprites. See here for more information.
    3. Merge your lower fence and upper fence tiles into a single rectangular tile in the Sprite Editor. Adjust the pivot point so that it sits at the center of your lower tile and redo the custom collision shape. This step is necessary so that your character doesn't pop half way thru the fence. It will also save you some sanity when designing the map.

    rectangular fence tile with adjusted custom pivot