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);
}
}
}