# How to allow player move diagonally on a 2D isometric map

i m trying to create a game that when on battle will be on a isometric tile map, i would like to have the player move around grid by grid, however my script below only able to move horizontal or vertical, when click on a diagonal direction, its not moving.

currently only able to move like below picture

how can i adjust the diagonal direction in the code ?

``````   void HandleMovement()
{
// Move the player towards the move point
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime);

// Check if the player has reached the move point
if (Vector3.Distance(transform.position, movePoint.position) <= 0.05f)
{
Vector3 moveDirection = Vector3.zero;

if (Input.GetMouseButtonDown(0))
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 currentPos = transform.position;
Vector3 direction = (mousePosition - currentPos).normalized;

// Calculate the magnitude of the direction to determine if it's a diagonal move
float absX = Mathf.Abs(direction.x);
float absY = Mathf.Abs(direction.y);

if (absX > absY)
{
// Horizontal movement (right or left)
moveDirection = new Vector3(Mathf.Sign(direction.x) * 1f, 0f, 0f);
}
else if (absX < absY)
{
// Vertical movement (up or down)
moveDirection = new Vector3(0f, Mathf.Sign(direction.y) * 0.5f, 0f);
}
else
{
// Diagonal movement
moveDirection = new Vector3(Mathf.Sign(direction.x) * 0.5f, Mathf.Sign(direction.y) * 0.25f, 0f);
}

Vector3 targetPosition = movePoint.position + moveDirection;

// Adjust targetPosition to align with grid
targetPosition = new Vector3(
Mathf.Round(targetPosition.x * 2) / 2,
Mathf.Round(targetPosition.y * 4) / 4,
0);

// Check if the target position is blocked
if (!Physics2D.OverlapCircle(targetPosition, 0.2f, whatStopsMovement))
{
movePoint.position = targetPosition;
}
}
}
}
``````

– This is a bit of a long and detailed answer for you to find an actual solution by yourself. Just write the algorithm first using code comments then do the actual work under those code comments, it’ll be easier

While you using comments to explain your code is cool, you don’t seem to have created the algorithm for your solution properly.

• You move the player before reading the input in the same method. Normally we’ll want to read the input first and then call a separate method to handle the movement
• You want a grid based movement but your target position is calculated in world units and never converted to cell units
• You don’t need to do crazy calculations just to find target position; it’ll only ever be player’s initial cell ±1 cell

Lets do this together:

1. You’ll read player’s input in the Update method(or in a method called inside the Update method)
2. Determine where the character should move based on input in a separate method when there is input. Since you want a grid based movement, your target position will always be a cell that is not blocked by a tree etc.
3. Move the player smoothly towards the target cell/position in a separate method if the player can move to the target cell

Some more tips on No2:

• You should use oneOfYourTilemapsOrGrid.WorldToCell() method to find the target cell or player’s cell. You can then convert the cellPosition back to world position with CellToWorld method to get a cell centered world position(I don’t remember exactly but you may need to add cellSize.y*0.5f to get the actual cell center, because iirc CellToWorld returns cell’s pivot point, which is the lowest point of a cell in isometric tilemaps)
• Your target world position will be found by adding direction.normalized*cellSize to player’s position and converting it to cell position and back to world position. cellSize is the world size of each cell in your tilemap/grid and it is found by your tilemap or grid.cellSize. We multiply the direction with cellSize to make sure the direction is always 1 cell wide/tall

Cheers,
let me know if you worked on the solution using my suggestions and still cant get it to work