- Home /
Clamping Rotation
Hi everybody! I want to create specific tranformation of the camera: when I press GUI buttons, the camera moves up/down (some kind of a zoom) and at the same time it is rotated from X=90 to X=50 and back again. The problem is that I can't clamp this range of rotation properly. Here is what I have:
function Update()
{
transform.localPosition = Vector3( 0, Mathf.Clamp(transform.localPosition.y, MinY, MaxY), Mathf.Clamp(transform.localPosition.z, MinZ, MaxZ));
transform.eulerAngles = Vector3(Mathf.Clamp(transform.eulerAngles.x, 50.0, 90.0), 0, 0);
}
function OnGUI()
{
var movement = new Vector3( 0, -60, 27);
if (GUI.RepeatButton(Rect(ButZIposX,ButZIposY,ButZIwidth,ButZIheight), "Zoom In"))
{
transform.Translate(movement * Speed * Time.deltaTime);
transform.Rotate(Vector3(-1,0,0) * Time.deltaTime);
}
if (GUI.RepeatButton(Rect(ButZOposX,ButZOposY,ButZOwidth,ButZOheight), "Zoom Out"))
{
transform.Translate(movement * -Speed * Time.deltaTime);
transform.Rotate(Vector3.right * Time.deltaTime);
}
}
Is there any suggestions?
Answer by RexGrey · Mar 16, 2012 at 05:36 PM
It is as easy as a Goo pie))
if (GUI.RepeatButton(Rect(ButZIposX,ButZIposY,ButZIwidth,ButZIheight), "Zoom In"))
{
if (transform.eulerAngles.x > 50.0)
transform.Rotate(-Vector3.right * Time.deltaTime);
}
if (GUI.RepeatButton(Rect(ButZOposX,ButZOposY,ButZOwidth,ButZOheight), "Zoom Out"))
{
if (transform.eulerAngles.x < 86.0)
transform.Rotate(Vector3.right * Time.deltaTime);
}
The problem is that Unity reacts incorrectly on values of rotation: after reaching a value of about 87, it shows exactly 90, so when I wrote "transform.eulerAngles.x < 90.0" it stopped rotating without reaching required value
Anyway, thanks for help
Answer by Berenger · Mar 13, 2012 at 06:27 PM
You should not rotate and translate in OnGUI, as it is called several times per frame. Besides that, the code seems ok. You can try something different, a lerp between the current rotation and a rotation of 50 (or 90 if going the other way).
PS : -Vector3.right == Vector3(-1,0,0)
Well, I tried $$anonymous$$athf.LerpAngle, and what I have now is that:
function Update()
{
if(zoo$$anonymous$$)
{
var anglein : float = $$anonymous$$athf.LerpAngle(90.0, 50.0, Time.time * 0.05);
transform.eulerAngles = Vector3(anglein, 0, 0);
}
if(zoomout)
{
var angleout : float = $$anonymous$$athf.LerpAngle(50.0, 90.0, Time.time * 0.05);
transform.eulerAngles = Vector3(angleout, 0, 0);
}
}
function OnGUI()
{
if (GUI.RepeatButton(Rect(ButZIposX,ButZIposY,ButZIwidth,ButZIheight), "Zoom In"))
zoo$$anonymous$$ = true;
if (GUI.RepeatButton(Rect(ButZOposX,ButZOposY,ButZOwidth,ButZOheight), "Zoom Out"))
zoomout = true;
}
Of course, it doesn't work either. Firstly, it continues to change rotation when I'm not pressing the button. Secondly, rotation starts with a small leap, but then it moves gradually. Thirdly, Time.time is not appropriate for my case.
$$anonymous$$ay there be other suggestions?
Your answer
Follow this Question
Related Questions
Camera global rotation clamping issue 0 Answers
3D top down rotation 1 Answer
Restricting GameObject Rotation 1 Answer
C# Clamp joystick rotation 0 Answers
GUI Rotation and Depth problem 1 Answer