- Home /
Limit object rotation on drag it
Hi!
My code allows click and drag on an object to rotate it applying a torque force (I use torque to obtain momentum/inertia).
Now I want to limit maximum and minimum degrees of the rotation, but I don't find the way.
Here is the code (js):
function Update () {
if(mouseDown==true){
//Get how far the mouse has moved by using the Input.GetAxis().
rotationX = Input.GetAxis("Mouse X") * sensitivityX;
rotationY = Input.GetAxis("Mouse Y") * sensitivityY;
//applying force
rigidbody.AddTorque(Vector3.up * rotationX * -1 * 10);
rigidbody.AddTorque(Vector3.right * rotationY * 10);
}
}
function OnMouseDown(){
mouseDown=true;
}
function OnMouseUp(){
mouseDown=false;
}
I tried to Clamp X, this works, but no how I expect:
public var Min_X_Clamp:float = 30; public var Max_X_Clamp:float = 150; function LateUpdate() { transform.rotation.eulerAngles = new Vector3( Mathf.Clamp(transform.rotation.eulerAngles.x, Min_X_Clamp, Max_X_Clamp), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z ); }
Also I tried getting "transform.eulerAngles.x" directly, but I don't obtain a correct solution.
Somebody know how to limit rotation in this case?
A lot of thanks!
Comment