Making character sprite face up/down

I’m freshly new to C#, and need a bit of help. Trying to get my character sprite to face the direction of it’s movement (at least up, right, left, and down), but can only get it to rotate left and right. The up/down portions only make it turn to the left. Any help is appreciated.

public class puppyControllerScript : MonoBehaviour
{

    public float maxSpeed = 10f;
    Quaternion theRotation;

    Animator anim;

    void Start ()
    {
        anim = GetComponent<Animator>();
        theRotation = transform.localRotation;
    }
   
    void FixedUpdate ()
    {

        //Movement
        float move = Input.GetAxis("Horizontal");
        float vmove = Input.GetAxis("Vertical");
        anim.SetFloat("Speed", Mathf.Abs(move+vmove));
        GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, vmove * maxSpeed);

        if (vmove > 0)
            Up();
        else if (vmove < 0)
            Down();
        else if (move > 0)
            Right();
        else if (move < 0)
            Left();
    }

    void Up()
    {
        theRotation.z = 90;
        transform.localRotation = theRotation;
    }

    void Down()
    {
        theRotation.z = 270;
        transform.localRotation = theRotation;
    }

    void Right()
    {
        theRotation.z = 0;
        transform.localRotation = theRotation;
    }

    void Left()
    {
        theRotation.z = 180;
        transform.localRotation = theRotation;
    }
}

Have you tried using the -180 to 180 scale instead of 0 to 360? Just a guess, not sure if that would make a difference here.

You’re saying if you press Up, the sprite faces left?

localRotation is a Quaternion, not a Vector3, so by modifying the “Z” value you are not modifying the angle.

Try this:

Vector3 rot = new Vector3(0f, 0f, 90f);
transform.localRotation = Quaternion.Euler(rot);
2 Likes

Ya, if I press up, left, or down, it faces left. Right faces right.
I tried changing to 180/-180 just to see, didn’t have an effect.

1 Like

Exactly what I needed, thanks. I’ll make sure to keep it in mind for next time.

1 Like