Does anyone know about the ability to limit how far an object can rotate in free space if you have control over it?
I don’t have a character controller… I simply have applied a script to my object because I’m already using a rigidbody.
It is flying in the air, as a glider, and I only want it to be able to tilt over so far. At the moment you can rotate it 360 degrees like a cartwheel, but I would like to lock it at a certain point.
If you’re using euler angles, its common to use Mathf.Clamp().
For instance, in the default mouse orbit script they use a function called ClampAngle to limit the rotation on the y axis so you can’t spin around in circles. Here’s a truncated version for illustrative purposes:
public var rotSpeed : float = 120.0;
public var yMinLimit : float = -20;
public var yMaxLimit : float = 80;
private var yRot : float = 0.0;
function Start(){
var angles = transform.eulerAngles;
yRot = angles.y;
}
function Update(){
yRot -= Input.GetAxis("Mouse Y") * rotSpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
transform.rotation = Quaternion.Euler(y, 0, 0);
}
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);
}