- Home /
Camera rotation X-axis limits (using Right Stick)
Hello. I found many issues with Mouse using for rotation, but a little bit of Stick with problem solved.
Here is an issue:
I want to limit X-axis rotation (vertical). With mouse controlls it is easy: mouse position equal to rotation. With Stick: current rotation + new rotation.
Here is a code:
public float hRotationSensitivity = 50f;
public float vRotationSensitivity = 30f;
public float minAngle = -45.0f;
public float maxAngle = 45.0f;
void Update()
{
// CAMERA LOOKING
float angH = Input.GetAxis("RightH") * hRotationSensitivity * Time.deltaTime * 5;
float angV = Input.GetAxis("RightV") * vRotationSensitivity * Time.deltaTime * 5;
// CL -- Rotate horizontal
Vector3 newRotationH = new Vector3(0, angH, 0);
transform.localEulerAngles = transform.localEulerAngles + newRotationH;
// CL -- Rotate vertical
Vector3 newRotationV = new Vector3(angV, 0, 0);
//Vector3 totalRotationV = transform.localEulerAngles + newRotationV;
//totalRotationV.x = Mathf.Clamp(totalRotationV.x, minAngle, maxAngle);
//transform.localEulerAngles = totalRotationV;
transform.localEulerAngles = transform.localEulerAngles + newRotationV;
}
Camera looking is work perfectly, but has no limits. Code that is commented does not solve my problem. It makes some strange shaked movement.
Please, what I missed.
I want camera like in GTA V. :D
Answer by dom_cobb_22 · Dec 08, 2016 at 02:17 PM
Thanks for answers, but I make my own solution. =)
Here it is:
Vector3 newRotationV = new Vector3(angV, 0, 0);
transform.localEulerAngles = transform.localEulerAngles + newRotationV;
float x = transform.localEulerAngles.x;
if (x > 45 && x < 315)
{
if (x > 45 && x < 50)
{
transform.localEulerAngles = new Vector3(45, transform.localEulerAngles.y, 0);
}
else if (x > 310 && x < 315)
{
transform.localEulerAngles = new Vector3(315, transform.localEulerAngles.y, 0);
}
}
It possible to change digits to named variables.
However, its absolutely 100% work =)
if (x > bottomAngle && x < topAngle)
{
if (x > bottomAngle && x < (bottomAngle + 5))
{
transform.localEulerAngles = new Vector3(bottomAngle, transform.localEulerAngles.y, 0);
}
else if (x > (topAngle - 5) && x < topAngle)
{
transform.localEulerAngles = new Vector3(topAngle, transform.localEulerAngles.y, 0);
}
}
Answer by AdhikS · Dec 06, 2016 at 07:03 AM
try this if(camera angleY is < 45 && camera angleY is > -45 )
{ Vector3 newRotationV = new Vector3(angV, 0, 0); }
Nope... Doesn't work. Camera moves until max or $$anonymous$$ position and frozen in this.
ideally it should work.check your camera angles at runtime and if at runtime your camera angle exceeds 45 or not. i think camera stop working coz angle of camera exceeds 45.check if this is the case
I made a mistake (you too). Typed 'Y' ins$$anonymous$$d 'X'... and used localEuler and usual Euler both ins$$anonymous$$d one of this. But now, after correcting, camera not moving at all. (Vertical)
Code looks like this:
float angH = Input.GetAxis("RightH") * hRotationSensitivity * Time.deltaTime * 5;
float angV = Input.GetAxis("RightV") * vRotationSensitivity * Time.deltaTime * 5;
// CL -- Rotate horizontal
Vector3 newRotationH = new Vector3(0, angH, 0);
transform.localEulerAngles = transform.localEulerAngles + newRotationH;
// CL -- Rotate vertical
if (transform.localEulerAngles.x < maxAngle && transform.localEulerAngles.x > $$anonymous$$Angle)
{
Vector3 newRotationV = new Vector3(angV, 0, 0);
transform.localEulerAngles = transform.localEulerAngles + newRotationV;
}
Debug.Log("transform.localEulerAngles " + transform.localEulerAngles);
Log message is here: transform.localEulerAngles (359.8, 357.8, 0.0)
It is '359.8' value. Not '-0.2' We are using $$anonymous$$Angle = -45 and maxAngle = 45
So what we have to do?
sorry for the last mistake try $$anonymous$$ angle 315 ins$$anonymous$$d of -45
check this link http://answers.unity3d.com/questions/1087351/limit-vertical-rotation-of-camera.html i hope this will solve your issue
Trying to do like in a link. Camera moves from 0 to 45 just wonderful, but then I turn from 45 to 0 and I reach 0, its reset to 45 (face to floor). I absolutely not understand how two same copy of code may behave differently.
// Try clamping after apply rotation
//transform.localEulerAngles = new Vector3($$anonymous$$athf.Clamp(transform.localEulerAngles.x, $$anonymous$$Angle, maxAngle), 0, 0);
if (transform.localEulerAngles.x > maxAngle)
{
transform.localEulerAngles = new Vector3(maxAngle, 0, 0);
}
else
{
if (transform.localEulerAngles.x < $$anonymous$$Angle)
{
transform.localEulerAngles = new Vector3($$anonymous$$Angle, 0, 0);
}
}
I try commented part first, then second part (uncommented now). Result is a same.
localEulerAngles never uses values with $$anonymous$$us sign. Therefore then it is reach zero and became 359.9, that is going to reset to 45.