Hello,
I have a small issue with my diagonal movement in my 2D top-down game. I’m using the new Input System for my movement and an Animator Blendtree for animations. I want 8-directional movement for my character.
The movement itself works well, but my main problem is that whenever I walk diagonally and release the hotkeys, the character can’t maintain or store the diagonal movement. This happens because I rarely/never release the hotkeys at exactly the same time, and for a tiny moment, the game thinks I’m only moving in one direction, like up, for example. As a result, the data for the last movement isn’t correct.
I’m still quite new to Unity and programming, and I’m learning everything on my own. Maybe someone is familiar with this issue, or there might be a solution in Unity that I’m not aware of.
Movement codesnip:
private void Update()
{
Animator();
Move();
mouseControl = Mouse.current.rightButton.isPressed;
if (mouseControl)
{
UpdateMouseControl();
moveSpeed = 1f;
}
else
{
UpdateVisualCubeDirection();
moveSpeed = 3f;
}
}
private void FixedUpdate()
{
if (moveDirection != Vector2.zero)
{
lastMoveDirection = moveDirection;
}
}
private void Move()
{
smoothedMovementInput = Vector2.SmoothDamp(smoothedMovementInput, moveDirection, ref movementInputSmoothVelocity, 0.1f);
rb.velocity = smoothedMovementInput * moveSpeed;
}
private void Animator()
{
animator.SetFloat("moveDirectionX", moveDirection.x);
animator.SetFloat("moveDirectionY", moveDirection.y);
animator.SetFloat("moveMagnitude", moveDirection.magnitude);
animator.SetFloat("lastmoveDirectionX", lastMoveDirection.x);
animator.SetFloat("lastmoveDirectionY", lastMoveDirection.y);
}
private void OnMove(InputValue movementValue)
{
moveDirection = movementValue.Get<Vector2>();
}
Blendtree: