Godot Version
4.6 stable
Question
Heya, i'm trying to make a Mario sort of game on my journey of learning Godot. I've implemented the camera, coins, movement and item blocks and it works pretty well, however i wanted to do the mushroom as well. The issue regarding the mushroom is that i'm trying to make it change movement direction everytime it hits a wall, just like in the original Super Mario Bros. However, this isn't working, as i'm using a TileMapLayer, and the mushroom will change direction upon contact with the TileMapLayer even if it's not touching the wall, but instead the floor. How would I make it detect the wall only? Thanks for any help!

Code:
extends RigidBody2D
@onready var Direction := 75
func _physics_process(delta: float) → void:
linear_velocity = Vector2(Direction, linear_velocity.y)
func _hit(body: Node2D) → void:
if body is CharacterBody2D:
queue_free()
elif body is TileMapLayer:
print(“yes”)
Direction = -Direction
I used a raycast pointed at the direction of travel coupled with tileset tile custom data.
When your mushroom raycast hits the tilemaplayer you get the name of the tile and act on that.
The custom data for the tile is called “name”. source_id is simply is found in the tileset.
func get_tile_name(tml:TileMapLayer, loc:Vector2)->String:
var tile:Vector2i = Globals.get_tile_at_global_pos(loc, tml)
var source_id:int = Globals.get_source_id(tml, loc)
var tileset:TileSet = tml.tile_set
var source:TileSetAtlasSource = tileset.get_source(source_id)
if source:
var tile_data:TileData = source.get_tile_data(tile, 0)
return tile_data.get_custom_data("name")
else:
# there is a problem getting the tile name
assert(false)
return ""
#globals
func get_tile_at_global_pos(global_pos: Vector2, tml:TileMapLayer) -> Vector2i:
# Convert global position to the TileMapLayer's local coordinates
var local_pos:Vector2 = tml.to_local(global_pos)
# Convert local coordinates to map/grid cell coordinates (Vector2i)
var map_coords:Vector2 = tml.local_to_map(local_pos)
# Retrieve tile identifiers
var source_id:int = tml.get_cell_source_id(map_coords)
var atlas_coords:Vector2i = tml.get_cell_atlas_coords(map_coords)
if source_id != -1:
return atlas_coords
return Vector2(-1, -1)
func get_source_id(tml:TileMapLayer, global_pos:Vector2)->int:
# Convert global position to the TileMapLayer's local coordinates
var local_pos:Vector2 = tml.to_local(global_pos)
# Convert local coordinates to map/grid cell coordinates (Vector2i)
var map_coords:Vector2 = tml.local_to_map(local_pos)
return tml.get_cell_source_id(map_coords)