- Home /
Limit Rotations with Min and Max on Mobile
This script will rotate an object when you move your finger on a touch screen device. All works fine, but how can I limit the angles to a minimum and maximum like the mouse look script does?
Any help would be greatly appreciated.
#pragma strict
private var rotate : float;
private var horozontalSpeed : float = 2.0;
private var verticalSpeed : float = 2.0;
var minimum : float;
var maximum : float;
function Update()
{
if (Input.touchCount == 1)
{
var touch : Touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
rotate = horozontalSpeed * touch.deltaPosition.x ;
transform.Rotate( 0, 0, -rotate, Space.World );
rotate = Mathf.Clamp (rotate, minimum, maximum);
}
}
}
Comment
Best Answer
Answer by whydoidoit · Apr 20, 2013 at 07:39 AM
Here's my code for clamping an angle:
float ClampAngle(float angle, float min, float max)
{
bool inverse = false;
var tmin = min;
var tangle = angle;
if(min > 180)
{
inverse = !inverse;
tmin -= 180;
}
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
var result = !inverse ? tangle > tmin : tangle < tmin;
if(!result)
angle = min;
inverse = false;
tangle = angle;
var tmax = max;
if(angle > 180)
{
inverse = !inverse;
tangle -= 180;
}
if(max > 180)
{
inverse = !inverse;
tmax -= 180;
}
result = !inverse ? tangle < tmax : tangle > tmax;
if(!result)
angle = max;
return angle;
}
Your answer
Follow this Question
Related Questions
Stopping rotation at 90° and -90°/270°. 0 Answers
Lock Z Rotation. Character Controller. 1 Answer
Rotating a GUi Texture based on Touch 1 Answer
Limiting rotation of object, specifically using scroll wheel 2 Answers
Limit GUI Rotation? 2 Answers