Rotating on mouse drag

Hey I’m prototyping a game that’s kind of like a Rubix cube but covered in letters.

You’ll notice right at the end I can’t rotate on one axis.

using UnityEngine;
using System.Collections;

public class rubixScript : MonoBehaviour
{

	public int speed;
	public float friction;
	public float lerpSpeed;
	public float xDeg;
	public float yDeg;
	public float zDeg;
	Quaternion fromRotation;
	Quaternion toRotation;

	void Update ()
	{
		if (Input.GetMouseButton (0)) {
			Debug.Log((Mathf.Cos (xDeg/60)*10));
			xDeg -= Input.GetAxis ("Mouse X") * speed * friction;
			yDeg += Input.GetAxis ("Mouse Y") * (Mathf.Cos (xDeg/60)*speed);
			transform.eulerAngles = new Vector3(yDeg,xDeg,zDeg);
		}
	}
}

It took me a while to figure out I was supposed to use Mathf.Cos(); for the yDeg, but what am I supposed to do with zDeg?

That’s called gimbal lock.

Use Quaternions instead to avoid gimbal lock easily.

You’ll be appending rotations… * is how you append a quat onto another.

transform.rotation *= Quaternion.AngleAxis(30, Vector3.right);//adds 30 degrees rotation around the x-axis