Clamping euler angles..

Hey guys, how do you clamp an euler angle?

I have this.

	// Called on every frame. Handles touch data and rotates Cube.
    void Update()
	{
        int fingerCount = 0;
		Vector3 CurrentFingerPos;   

		foreach (Touch touch in Input.touches)
		{
            if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled)
			{
                fingerCount++;
            
        	}
	        if (fingerCount > 0)
			{
				if(fingerCount > 0  fingerCount < 2)
				{
					CurrentFingerPos = touch.deltaPosition;
					
					// Sets our finger movement to object rotation.
					
					if(Vector3.Dot(transform.forward, new Vector3(0.0f,1.0f,0.0f)) < 0.5)
					{
						z += CurrentFingerPos.x * speed;
					}
					else
					{
						z -= CurrentFingerPos.x * speed;
					}
					y += CurrentFingerPos.y * speed;
					x = 0;
					
					// Converts our numbers into euler angles.
            		Quaternion rotation = Quaternion.Euler(y, x, z);

					// Sets our new rot.
					transform.rotation = rotation;
				
				}
				
			}
			else
			{
				return;
			}
    	}
	}

It works… I’m trying to make it so that whatever way the object is facing… whether upside down or rightside up, when you move your finger right, it will still spin right… The thing is that that suddenly the controls will ‘jump’ to moving the other way, really confusing you.

Is there a way to lock the Y axis so it doesn’t pass 180 degrees?

Thanks

Did it… Just need to balance. Thanks anyway guys. For anyone else :slight_smile: don’t read old posts pre 2012. They wont give the correct code as Mathf.Clamp now needs to be assigned to a variable to reference, not directly to a return value.

    void Update()
	{
        int fingerCount = 0;
		Vector3 CurrentFingerPos;   

		foreach (Touch touch in Input.touches)
		{
            if (touch.phase != TouchPhase.Ended  touch.phase != TouchPhase.Canceled)
			{
                fingerCount++;
            
        	}
	        if (fingerCount > 0)
			{
				if(fingerCount > 0  fingerCount < 2)
				{
					CurrentFingerPos = touch.deltaPosition;
					
					// Sets our finger movement to object rotation.
					
					z += CurrentFingerPos.x * speed;
					y += CurrentFingerPos.y * speed;
					x = 0;
					
					y = Mathf.Clamp(y, 0, 180);
					
					// Converts our numbers into euler angles.
            		Quaternion rotation = Quaternion.Euler(y, x, z);

					// Sets our new rot.
					transform.rotation = rotation;
					//transform.rotation.y = Mathf.Clamp(transform.rotation.y -45,45);
				
				}
				
			}
			else
			{
				return;
			}
    	}
	}