Make 2D Sprite face direction of last movement when idle

Hedy guys, I am new to Unity and scripting and am working on developing a 2D top down style game. I have the animations and movement good to go but I am struggling with getting the character to face the last direction of movement when idle. I can’t seem to figure it out. Here is the movement code I have:

public class Movescriipt : MonoBehaviour
{
    public float moveSpeed = 5f;

    public Rigidbody2D rb;
    public Animator animator;

    Vector2 movement;

    // Update is called once per frame
    void Update()
    {
        movement.x = UnityEngine.Input.GetAxisRaw("Horizontal");
        movement.y = UnityEngine.Input.GetAxisRaw("Vertical");
        animator.SetFloat("Horizontal" , movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);

      

       

    }
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime); 
    }
}
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);

        if (movement != Vector2.zero)
            rb.MoveRotation(Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg);
    }

you need to create a variable for which direction your facing

for example

public byte chardirection = 0;

where 0 = left, 1 = right, 2 = up, etc, and when you move make sure to set this direction according to your inputs

How do I assign them to an input?

I did this but when I let go of the directional input it reverts back to my default idle and not the direction of m0ovement idle.

so what kind of top down is your game? do you want to rotate like a car, or is it like an rpg?

Its an rpg. So like how in pokemon you can just tap a direction and the player sprite faces that way.

then you can do something like this:

if( movement.x > 0)
{
chardirection =2;//2 = facing right
}

now when your animator goes back to idle you should check the value of chardirection to pick the correct animation

Would I need to do a public byte for each direction?

no you just change the value, its for your decision what each value means

for example you can decide that 0 = left, and 2 = right

and then on your animator when the value is = 2 then you switch to the idle right

You add new behaviour you have not previously asked for and say the code I provided that gives you what you originally asked for doesn’t work?

As I hope you can see in the example I provided, when there’s no input, it doesn’t change the direction so…

… the code I provided just does that because when there is no input, there’s no direction from the input. If you want a specific direction when that’s the case then add the code there.

If you are asking for others to write code for you as you add behaviours you need then that’s not what the forum is for.

i dont think he wants to rotate object

Hmm, well that was clear at all! I guess because 99% of the time this question means rotate in the direction of movement so that’s what I went with.

Thanks for clarifying.

So to be clear, you want to choose one of 4 directions and set your sprite/animation accordingly?

If so, the basic idea is…

using UnityEngine;

public class FaceDirection : MonoBehaviour
{
    public float moveSpeed = 5f;

    public enum Facing
    {
        Idle,
        Left,
        Right,
        Up,
        Down    
    }

    private Vector2 movement;
    private Rigidbody2D rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        var facing = GetFacing();
    
        /* Change the animation or whatever here based upon the current direction */
    }

    private Facing GetFacing()
    {
        if (movement.x < 0f)
            return Facing.Left;
    
        if (movement.x > 0f)
            return Facing.Right;
    
        if (movement.y < 0f)
            return Facing.Down;
    
        if (movement.y > 0f)
            return Facing.Up;
    
        return Facing.Idle;
    }
 
    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

There are far better ways of doing this stuff depending on your experience with Unity and other requirements (maybe 8 directions etc) but this is the basic idea made as simple as possible.

Maybe you want something else, hard to say for sure.