Godot Version
4.5.1
Goal
I’m building a Mining Game prototype similar to what Aarimous did in his Youtube channel. I want to use TileMapLayer to place mineable ground blocks. Each ground block has a different type (ex. dirt, iron, gold, etc.). I want the Player to detect that there is a ground block next to them and then mine it.
Problem
I’m trying to use Raycast2D to detect a TileMapLayer so I can find the map position and get the corresponding cell that is at the atlas_coordinates. When the Player faces “right” or “down” I get correct atlas_coordinates for the Ground blocks.
However when I face the Player the the “left”, collider.get_cell_atlas_coords results in a (-1,-1) which means that the result is “No cell exists.”
Player Character code
#Player Character code for changing Sprite direction and PickaxeRayCast direction
func _physics_process(delta: float) -> void:
# Change direction based on Input left or right
if Input.is_action_just_pressed("left"):
$AnimatedSprite2D.flip_h = true
$PickaxeRayCast.set_target_position(Vector2(-12.0,0.0))
if Input.is_action_just_pressed("right"):
$AnimatedSprite2D.flip_h = false
$PickaxeRayCast.set_target_position(Vector2(12.0,0.0))
if Input.is_action_just_pressed("down"):
$PickaxeRayCast.set_target_position(Vector2(0.0,12.0))
PickaxeRayCast code
#PickaxeRayCast code for trying to detect a TileMapLayer
extends RayCast2D
#@onready var pickaxe = self
var collider
func _process(_delta: float) -> void:
if self.is_colliding():
print(str(self.is_colliding()) + " is colliding.")
if self.get_collider() is TileMapLayer:
collider = self.get_collider()
print(str(collider) + " is collider")
var map_position = collider.local_to_map(self.get_collision_point())
print(str(map_position) + " is map position")
print(collider.get_cell_atlas_coords(map_position))
else:
print("Not colliding")
Clearly a cell exists so either I am implementing Raycast2D incorrectly or using the wrong tool for the job. I have searched the forums and Google and haven’t encountered another person having the problem.
- What am I doing wrong?
- What do I need to change to get the behavior I want?
