Multiple Button Presses Not Working

Hello,

I’m trying to rotate a sprite along the z axis using keyboard input and input actions. The action for all of them is set to pass through and the control type is button. The composite type for each is 1D Axis and the min/max values are -1 and 1 respectively. When I code my script to implement the rotation, the keys work individually. The problem I am having is wanting to press two keys at once. Only the first switch statement is able to rotate the sprite when pressing two keys and the second switch doesn’t function the same. When I put one switch above the other one the problem reverses. What ways can I solve this so that both switch statements allow multiple keypresses?


using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerController : MonoBehaviour {
    private PlayerControls playerControls;
    private float xRightInput, yRightInput;


    private void Awake() {
        playerControls = new PlayerControls();
    }

    private void OnEnable() {
        playerControls.Enable();
    }

    private void OnDisable() {
        playerControls.Disable();
    }

    // Update is called once per frame
    void Update() {
        GetPlayerInput();
    }

    //Read the movement value
    private void GetPlayerInput() {

        xRightInput = playerControls.Movement.HorizontalRotation.ReadValue<float>();
        yRightInput = playerControls.Movement.VerticalRotation.ReadValue<float>();

    }

    private void FixedUpdate() {

        //Horizontal & Vertical Transformations
        switch (yRightInput) {
            case -1:
                DownRotate();
                break;
            case 1:
                UpRotate();
                break;
            default:
                break;
        }
        switch (xRightInput) {
            case -1:
                LeftRotate();
                break;
            case 1:
                RightRotate();
                break;
            default:
                break;
        }
    }

    private void LeftRotate() {
        transform.rotation = Quaternion.Euler(0, 0, 90);

        if (yRightInput == -1) {
            transform.rotation = Quaternion.Euler(0, 0, 180);
        } else if (yRightInput == 1) {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }
    }

    private void RightRotate() {
        transform.rotation = Quaternion.Euler(0, 0, 270);

        if (yRightInput == -1) {
            transform.rotation = Quaternion.Euler(0, 0, 180);
        } else if (yRightInput == 1) {
            transform.rotation = Quaternion.Euler(0, 0, 0);
        }
    }

    private void DownRotate() {
        transform.rotation = Quaternion.Euler(0, 0, 180);

        if (xRightInput == -1) {
            transform.rotation = Quaternion.Euler(0, 0, 90);
        } else if (xRightInput == 1) {
            transform.rotation = Quaternion.Euler(0, 0, -90);
        }
    }

    private void UpRotate() {
        transform.rotation = Quaternion.Euler(0, 0, 0);

        if (xRightInput == -1) {
            transform.rotation = Quaternion.Euler(0, 0, 90);
        } else if (xRightInput == 1) {
            transform.rotation = Quaternion.Euler(0, 0, -90);
        }

    }

}

What kind of view is this? Why not just calc a rotation from the vectors and set the rotation once?

It’s a 2D scene. How would I go about doing that?

What is your desired movement like? It is top-down?

Yes it is top down. I was able to change the code to calculating the rotation based off a vector, but I would like to remove the 45 degree inbetween when you press two keys and just have it look in the cardinal directions.

public class PlayerController : MonoBehaviour {

    private Vector2 rightInput;

    private void GetPlayerInput() {
        rightInput = new Vector2(xRightInput, yRightInput);
    }

    private void FixedUpdate() {
        Vector3 currentRotation = Vector3.left * rightInput.x + Vector3.down * rightInput.y;
        Quaternion playerRotation = Quaternion.LookRotation(currentRotation, Vector3.forward);

        rigidb.SetRotation(playerRotation); //this is the Rigidbody2D 
    }
}

So you only want it to rotate to north, south, east, and west? You could take your playerRotation quaternion and snap it to the nearest 90 degrees. You would probably want conditions to decide which way to face when both are pressed though.