Restricting rotation to multiples of 15?

Just like how the snapping works in the editor, I want the rotation controls of this object to snap to increments of 15. Here’s what I’ve got right now:

var horizontalSpeed : float = 2.0;
var verticalSpeed : float = 2.0;

function FixedUpdate ()
	{
		transform.rotation.x = Mathf.Clamp(transform.rotation.x, 0, .7);

    		if (Input.GetMouseButton(1))
    			{

        			var h : float = horizontalSpeed * Input.GetAxis ("Mouse X");
					var v : float = verticalSpeed * Input.GetAxis ("Mouse Y");
					
					transform.Rotate (v, 0, 0);
			    }
    
    		if ((Mathf.Round(transform.eulerAngles.x)) % 6 == 0)
				{
    				print("This value is a multiple of 15.");
						}else{
					print("This value isn't a multiple of 15.");
				}

	}

I’m using the modulo to detect when the x angle is at a multiple of 15, but I don’t know how to restrict it exclusively to those values.

That can't work anyway...transform.rotation is a quaternion, which is 4-dimensional and doesn't use degrees. You can use eulerAngles, but as the docs say, you should set all elements at once and not just .x.

1 Answer

1

Mathf.Round(angle / 15.0) * 15.0