Godot Version
v4.3.stable.mono.official
Question
Hello, I’m quite new to Godot and decided to follow this tutorial for a point and click game: https://www.youtube.com/watch?v=DkAmGxRuCk4&t=609s
However, I’m encountering an issue where if the Sprite2D is placed at the center of the cell and not the grid, when it moves, it moves at the edges of the cells instead of their center, and I’m not entirely sure what causes it.
Also, sometimes, if I click on the edges of the cell instead of its center, the sprite gets “stuck” in between cells instead of completely going to the center of the next cell. It usually happens if the cell I click is adjacent to where the player is currently at.
Here’s what it looks like: AStarGrid and snapping position - Album on Imgur
I’ve tried to translate the GDScript code into C#, so I may have screwed up somewhere, but I can’t figure where. If anyone can help me understand this better, I’d appreciate it very much.
public partial class Player : Node2D
{
AStarGrid2D astar_grid;
TileMapLayer tilemap_layer;
Array<Vector2I> id_path;
Array<Vector2I> current_id_path;
Vector2 target_position;
public override void _Ready()
{
tilemap_layer = GetNode<TileMapLayer>("../TileMapLayer_TEST");
astar_grid = new AStarGrid2D();
astar_grid.Region = tilemap_layer.GetUsedRect();
astar_grid.CellSize = new Vector2(16, 16);
astar_grid.DiagonalMode = AStarGrid2D.DiagonalModeEnum.OnlyIfNoObstacles;
astar_grid.Update();
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton inputEvent && inputEvent.IsAction("move")) PlayerInput();
}
public override void _PhysicsProcess(double delta)
{
if (current_id_path == null || current_id_path.Count == 0) return;
target_position = tilemap_layer.MapToLocal(current_id_path[0]);
GlobalPosition = GlobalPosition.MoveToward(target_position, 1);
if (GlobalPosition == target_position) current_id_path.Remove(current_id_path[0]);
}
public void PlayerInput()
{
id_path = astar_grid.GetIdPath(
tilemap_layer.LocalToMap(GlobalPosition),
tilemap_layer.LocalToMap(GetGlobalMousePosition())
).Slice(1);
if (id_path != null || id_path.Count != 0) current_id_path = id_path;
}