Hi as you can see on the video attached, I'm trying to destroy tiles when my Raycast2D collides with one tile,and it is working when I face tiles on the right side or downside, but not for left side or upwards 🤯 . I don't really know why this is happening 🧐, maybe for a negative value of my raycast? I'm just rotating it depending on which direction the player faces when moving.
#Mining Game, a RayCast2D is used for destroying tiles, but only works on right and down sides. Why?
16 messages · Page 1 of 1 (latest)
Have you tried putting print(pos_cell) at the end of the raycast code to see what tile is being targetted?
Also get_collision_point returns a global position. The documentation for local_to_map covers this:
Vector2i local_to_map ( Vector2 local_position ) const
Returns the map coordinates of the cell containing the given local_position. If local_position is in global coordinates, consider using Node2D.to_local before passing it to this method. See also map_to_local.
So that should be I think:
real_position = tilemap.to_local(raycast.get_collision_point())
Hi thanks for your reply. Yes I tried the print(pos_cell) and what I can see is that sometimes the tile on upside and leftside (the ones that fails) are the same numbers , like for example (1, 2).
I tried with the new line real_position = tilemap.to_local(raycast.get_collision_point()) but the result is the same, tiles on upside and leftside keep failing on being destroyed, nothing happens.
I'm very newbie so this is makes me crazy 😅 I keep searching the issue..
I think I know what is happening. I bet if you check pos_cell when the raycast is hitting the tiles to the left or above it is actually giving the tile your character is in. The collision point is just within the tile your character is in, where as the collision point for the right and below tiles is just inside the adjoining tiles and so they are working correctly.
I would try a different approach. If the raycast is colliding then erase the tile in the tilemap that is the player's position plus the directional offset of the raycast. Perhaps something like this:
If raycast.is_colliding():
tilemap = raycast.get_collider() as TileMap
tilemap.erase_cell(tilemap.local_to_map(position + Vector2(32.0, 0.0).rotated(raycast.rotation)))
You would need to change the 32.0 in the Vector2 to be the width of a tile in pixels.
@delicate rain I did the changes but with that new code no tile is destroyed, even on downside or rightside that was working before. But thanks for your interest and help !! If you ever need help with animation tell me, I'm a professional Spine 2D animator ^^ Will keep trying..🧐
OK, it's a shame that didn't work. I assumed the script was on your CharacterBody2D and the start of the raycast was at its origin. If not that may be why the position is not correct.
it was not at origin, just in the middle of the char. I tried with origin but still don't make it work, a new error appear:
I meant having the RayCast2D at the origin of the CharacterBody2D, so its Transform position is 0.0, 0.0. Can you show me a screenshot of the CharacterBody2D so I can see how the nodes are childed to it?
Sure! Before I had it in the red arrow but now I moved to Player 0,0
I sent you the source project too in case you want to see it in detail
Well that took a while to track down! I didn't realise you were rotating the Hand-Tool node and not the RayCast2D node. Put the Hand_Tool node back so it is in the center of the character and change the code to:
if raycast.is_colliding() : #and Input.is_action_just_pressed("Use_Tool"):
tilemap = raycast.get_collider() as TileMap
tilemap.erase_cell(-1, tilemap.local_to_map(position + Vector2(128.0, 0.0).rotated($Hand_Tool.rotation)))
Also change line 11 to:
pickaxe = $Hand_Tool/Pickaxe
and delete line 34 to stop the error messages.
Now is working perfectly!! 🥹 You are awesome! I owe you a big one, thank you very much, I will work now on the next parts of the game and animations. Again if you need help with animation on your game tell me anytime 🙏🏻