Ignore collisions with water if ground above water
I'd like to make a sort of a "bridge" where the player can walk over water, and I'm finding this to be extremely difficult.
In the above image, the water is on the physics layer "water", the grass is on the physics layer "ground" and that board is on the physics layer "ground" as well.
Since there is no water under the ground, the player can walk, and they crash when they hit the water, so that part is good.
Now the issue is that the ground layer overlaps with the water, and when the player attempts to walk on that board they "collide" with the water.
How can I prevent collisions with water if the water's collider is "masked" by something on the ground layer?
This is what my layer collision matrix looks like:
i have the same problem did you ever finde a solution
Answer by $$anonymous$$ · Feb 28, 2021 at 07:36 AM
I ran into the exact same issue. What I ended up doing for my project is: Make all my tiles Scriptable Tiles, and ovrride GetTileData() like this in the class (This function is used to copy data from your Tile asset to the Tilemap that contains it):
public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
tileData.sprite = this.sprite;
tileData.color = Color.white;
// Try to override colliderType of tileData of the current position
if (this.colliderType != ColliderType.None)
{
var grid = tilemap.GetComponent<Transform>();
// Find tilemaps that are on top of the "tilemap" (argument)
// In my setup I always have 2 tilemaps attached to the Grid.
// Last child always being the foreground.
// If I had a more complicated scenario I'd loop through all children and
// find those tilemaps that are on higher sorting layers.
var topTilemapObj = grid?.parent?.GetChild(grid.parent.childCount - 1);
var topTilemap = topTilemapObj?.GetComponent<Tilemap>();
// If I found a tilemap, check the tile on the exact same position
// If that tile has a "None" colliderType, I set the current tileData's
// colliderType to "None" as well.
if (topTilemap != null && topTilemap != tilemap.GetComponent<Tilemap>())
{
var tileOnTop = topTilemap.GetTile<Tile>(position);
if (tileOnTop?.colliderType == ColliderType.None)
{
tileData.colliderType = ColliderType.None;
return;
}
}
}
tileData.colliderType = this.colliderType;
}
Output:
,
Your answer
Follow this Question
Related Questions
Tilemap Collider problems 0 Answers
Rotate tilemap collider -1 Answers
Player slips through the tilemap collider 2D. 0 Answers
OnCollisionEnter2D not calling, but objects are colliding. 1 Answer