How to make tiles transparent when the player goes behind them

Godot Version



we have been trying to make roofs disapear when players go inside a house or something like that. the problem is there isnt any video regarding this in youtube for godot 4. can you guys help? and please be specific… think of me as an idiot who has never coded anything.

One solution would be to have roofs be on a separate TileMap layer, and when player enters the building, we turn that layer off.

set_layer_enabled (roof_layer, false) #where roof-layer - ID of roof layer (int)

Problem with this solution would be that if we have all roofs be on the same layer, turning one off would turn of other ones. Workaround would be to have each roof on separate layer, but that would get messy really quickly. If you dont care about seeing insides of other buildings, or if there isnt that much other buildings on-screen, this could work.

Other solution would be to have roofs be separate object and disable its visibility (hide()/show() methods or visibility = false/true).

For both of these solutions you would have to somehow detect if “player is inside the building”. Second solution is easier in a sense that you can check “If Area2D entered” and “is Area2D a player” since every roof is a separate object.

Last solution would be to use shaders, that would allow to modify which part of roof we hide. For example, we can hide not entire roof, but only circle inside it, or not hide it but make it half-transparent.

Which solution you want will depend on which implementation suits your game and seems the easier for your game.

1 Like

I’m not sure how can you implement such a thing, but using viewports can be a solution. Check this video from Raffaele Picca:

1 Like

Example code of how to make this solution, assuming that the roof has Area2D, player has Area2D and is in group “player”

extends Node2D

func _ready():
	#       ^ replace area with body if player has ..Body instead of Area2D
	#       ^ replace area with body if player has ..Body instead of Area2D

func _on_area_2d_area_entered(area):
	if area.is_in_group("player"):

func _on_area_2d_area_exited(area):
	if area.is_in_group("player"):
1 Like

thank you for this, but we are looking for something that would use Alpha modulation, so we can make it a bit transparent but yet visible at the same time. but if you dont have any suggestions i guess this the way.
also the solution for that i guess is both of the tilemap and player having area2d and using a trigger script.

also I used the code, and its giving me the error. I should define the layer using the @onready method, right? if so
@onready var backg=$back
should be right?