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;
}
}
}
}