RigidBody rotation contraints dont work at all

Hi

I have a simple scene with a cube balancing on a sphere. The cubes x and z are controlled by user input. Imagine a see saw but3d. Anyway, it all works fine except that the cube spins around on top of the sphere even though the Y rotation axis is constrained and at no point in code do I modify the Y axis.

Here is my script that is attached to the cube:

private float tiltSpeed = 3f;
private float tiltX, tiltZ;
private Vector3 localRotation;
Vector3 tempQt;
private Rigidbody rb;

// Use this for initialization
void Start () {
localRotation = transform.rotation.eulerAngles;
rb = GetComponent<Rigidbody> ();
tempQt = transform.rotation.eulerAngles;
}

// Update is called once per frame
void Update () {

}

void FixedUpdate(){
float curSpeed = Time.deltaTime * tiltSpeed;

//tiltX = Input.acceleration.z + 5f * 2f;
//tiltZ = Input.acceleration.x * curSpeed;

tiltZ = Input.GetAxis ("Vertical") * curSpeed;
tiltX = Input.GetAxis("Horizontal") * curSpeed;

localRotation.x += (float)tiltZ;
localRotation.z -= (float)tiltX;

tempQt = rb.rotation.eulerAngles + new Vector3(localRotation.x, 0f, localRotation.z);

rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles + new Vector3(localRotation.x, 0f, localRotation.z));

}

Is this a bug or am I doing it wrong?

Any help getting the Y axis constrained would be greatly appreciated.

Thanks

Ok

I think after some googling I figured it out.

It seems that you cant rotate an object on 2 axis independent of the third.
So what I was trying to do is physically impossible and was leading to gimbal lock.

Is there a way to rotate a plane on x and z while limiting how far it can spin around y?

What I am trying to achieve is have a cube, that is flattened (like a plane with a little height) tilt left when left key is pressed, right when right key, tilt forward with up key, tilt back with down key.

All of these work fine on their own. The trouble starts when you press left the up it start to spin around y.

I want it so that on a phone when you tilt the phone you have to try keep the plane level without the plane spining more than it has to.

Hope all that made sense :stuck_out_tongue: