Answers for "How do I clamp the Z-Axis Rotation for this code?"
http://answers.unity.com/questions/934609/how-do-i-clamp-the-z-axis-rotation-for-thsi-code.html
The latest answers for the question "How do I clamp the Z-Axis Rotation for this code?"Answer by fafase
http://answers.unity.com/answers/934621/view.html
Lucky you I did one similar thing lately:
public void RotateShip(Vector3 tapPosition)
{
float polarity = (tapPosition.x < halfWidth) ? 1f : -1f;
transform.Rotate(Vector3.forward, polarity * Time.deltaTime * speed);
float angle = transform.eulerAngles.z;
transform.eulerAngles = new Vector3(0f, 0f, ClampAngle(angle, -45f,45f));
}
private float ClampAngle(float angle, float min, float max)
{
if(angle < 90f || angle > 270f)
{
if(angle > 180)
{
angle -= 360f;
}
if(max > 180)
{
max -= 360f;
}
if(min > 180)
{
min -=360f;
}
}
angle = Mathf.Clamp (angle, min, max);
if(angle < 0)
{
angle += 360f;
}
return angle;
}
tapPosition is the screen position of the touch as it was for our project, in your case, I guess it will be the touch position.
The parameters in ClampAngle define the range of rotation, in the example it is from -45 to 45 considering 0 to be the initial rotation.
The ClampAngle method comes from there: http://answers.unity3d.com/questions/141775/limit-local-rotation.html
you would have to change polarity maybe as in my case it was just if the touch was on left or right half of the screen.Sat, 28 Mar 2015 11:58:44 GMTfafase