# How to limit the rotation of an object??

Yeah, I know I could use Mathf.Clamp, but the problem is that it goes in contrast with something else, so I wuold like to have a smoother effect. Have u got any idea??
THNKs.

Mathf.Clamp is the answer. Perhaps if you elaborate on “something else” we could help you figure out what to do?

// Hacked and gutted from the MouseOrbit script that comes with the Standard Package as well as the control script that comes with iPhone Basic. Combined with a script to tilt an object with the iPhone accelerometer.

``````var xSens = 1.0;
var ySens = 1.0;
var zMinLimit : float = -20;
var zMaxLimit : float = 80;
var xMinLimit : float = -20;
var xMaxLimit : float = 80;
var angleComp : float = 0.5;

private var x = iPhoneInput.acceleration.x;
private var y = iPhoneInput.acceleration.y;

function Start ()
{

for (var evt : iPhoneTouch in iPhoneInput.touches)
{
if (evt.phase == iPhoneTouchPhase.Moved)
{
normalizedSpeed = evt.position.y / Screen.height;
x = transform.eulerAngles.x;
y = transform.eulerAngles.y;

}
}
}

function FixedUpdate () {

var accelerator : Vector3 = iPhoneInput.acceleration;

accelerator.y = ClampAngle((accelerator.y * ySens), xMinLimit, xMaxLimit);
accelerator.x = ClampAngle(((accelerator.x + angleComp) * xSens), zMinLimit, zMaxLimit);

var rot = Quaternion.Euler(accelerator.x, 0, accelerator.y);

transform.rotation = rot;
//Debug.Log (accelerator);

}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
``````

This script is something I cobbled together for an iPhone game; it takes the accelerometer input and clamps the rotation to the values defined in x/z Min/MaxLimit. You could add Y rotation with some small modification, and it is easily modifiable to take input from any source. Hope it points you in the right direction, just drop the script on the object whose rotation you want to limit.