- Home /
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.
yes, $$anonymous$$athf.Clamp would be probably the best, but I've the little problem that my object stops at the maximum or $$anonymous$$imum, but it shakes itself
That is actually why the if angle statements are in the function, to deal with that. The vibration occurs because without the if statements, Unity doesn't know how to deal with values above 360 or below -360, so you get a shimmer as it is trying to interpolate that values. The if statements make it so if you go above or below the max/$$anonymous$$ rotation, the angle value 'wraps' around in a loop.
Answer by testure · Jun 05, 2011 at 08:58 PM
Mathf.Clamp is the answer. Perhaps if you elaborate on "something else" we could help you figure out what to do?
Answer by noradninja · Jun 05, 2011 at 09:38 PM
// 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.
Your answer
Follow this Question
Related Questions
limit accelerometer controlled rotation 1 Answer
gesture rotation clamp 2 Answers
Controlling camera rotation limits. 1 Answer
Clamp Rotation Problem 1 Answer