How to add alternate idle directions to a top-down game
I am working on a top down game, and my player sprite and animation is finished. When there is no input, the sprite is supposed to enter an idle state, but facing the same direction. I have different animation sets for movement and idling, and I don't know how to code in the idle animations for facing different directions. The sprite still animates but doesn't move after input is released.
I don't know if I explained this correctly, so as an example I'll say the player is walking left. The left arrow key is released so there is no more input, and the sprite stops moving. However, the animation continues. I want to stop this animation and enter another animation, used only when there is no input but at the same time facing the same direction as the last input. Here is my code:
``` using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
movement = movement.normalized;
if (movement != Vector2.zero)
{
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
} ```
Thank you for any help!