Hey I have a small issue with my movement. I am working on a tilebased game similar to Bomberman so I created a tilebased movement. However my Problem is that if the character keeps the button pressed to move left / right and additionally presses the up/ down key the character will not move upwards instead he keeps on moving left/right.
At the same time when moveing up or downwards and simultaniously pressing left or right he moves to the left / right.
I guess the reason is in the if Statement since Input.x has a higher priority the System will always use this. How can I give the last Input always the highest prio ?
//Check if player is moving
if (isMoving)
{
Vector2 targetPosition = CurrentPosition + moveDirection;
t += Time.deltaTime * (moveSpeed / gridSize);
transform.position = Vector3.Lerp(gameManager.GetPosition(CurrentPosition, 1), gameManager.GetPosition(targetPosition, 1), t);
if (t >= 1)
{
isMoving = false;
t = 0;
CurrentPosition = targetPosition;
}
else
{
return;
}
}
moveDirection = Vector2.zero;
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
if (input.sqrMagnitude < 0.01f)
{
return;
}
if (input.x > 0)
{
moveDirection = Vector2.right;
}
else if (input.x < 0)
{
moveDirection = -Vector2.right;
}
else if (input.y > 0)
{
moveDirection = Vector2.up;
}
else if (input.y < 0)
{
moveDirection = -Vector2.up;
}
Vector2 targetPositionPrediction = CurrentPosition + moveDirection;
if (!gameManager.IsTileAccessible(targetPositionPrediction))
return;
isMoving = true;
}