I’m creating a game that requires the player to tilt a table to navigate a ball through a maze. I’ve gotten the table to rotate around the Z and X axes as I need them to, however whenever I switch which axis the table is rotating around the table resets and rotates from the flat position. Then if I switch axes again the table resets to its most recent rotation around the current axis. Example: Z-rotation → attempt X-rotation → table resets → X-rotation. I apologize if this doesn’t make any sense.
public float rotationSpeed = 1;
public float resetSpeed = 10;
private Quaternion level;
private float minX = -30;
private float maxX = 30;
private float minZ = -30;
private float maxZ = 30;
private float rotX;
private float rotZ;
// Use this for initialization
void Start() {
level = transform.rotation;
}
// Update is called once per frame
void Update()
{
rotX += Input.GetAxis("RotateX") * rotationSpeed;
rotX = Mathf.Clamp(rotX, minX, maxX);
rotZ += Input.GetAxis("RotateZ") * rotationSpeed;
rotZ = Mathf.Clamp(rotZ, minZ, maxZ);
if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
transform.rotation = Quaternion.AngleAxis(rotX, Vector3.right);
rotX = Mathf.Clamp(rotX, minX, maxX);
}
if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow))
{
transform.rotation = Quaternion.AngleAxis(rotZ, Vector3.forward);
rotZ = Mathf.Clamp(rotZ, minZ, maxZ);
}