0
\$\begingroup\$

I use this script rotate the camera on its local X axis:

 float v = verticalSpeed * Input.GetAxis("Mouse Y");
 transform.Rotate(-v, 0, 0);

Right now this lets the player look up & down without limit, wrapping around a full 360 degrees. How can I limit this so they can look only 90 degrees up or 90 degrees down from the horizon?

\$\endgroup\$
  • \$\begingroup\$ Do you want to limit the speed of change over time between two limiting rates, or limit the absolute orientation between two limiting angles? If the latter, have you reviewed previous questions about limiting or clamping rotation angles in Unity? \$\endgroup\$ – DMGregory Aug 15 at 16:14
  • \$\begingroup\$ this for Fps camera but When I rotate to the top it is going 360 I want limit \$\endgroup\$ – NADER LABBAD Aug 15 at 16:18
  • \$\begingroup\$ This script Makes player looks top and down but There is no limit it reach to 360 I'm trying to make it between 90 -90 \$\endgroup\$ – NADER LABBAD Aug 15 at 16:23
0
\$\begingroup\$

This is a straightforward application of Mathf.Clamp(value, min, max). Ordinarily I'd recommend caution calculating new orientations from Euler angles, but for camera orientation it's typically safe enough. You might want to pull back slightly from 90, say 80 degrees, so you don't hit nasty behaviour right at the poles.

// Correct for deltaTime so your behaviour is framerate independent.
// (You may need to increase your speed as it's now measured in degrees per second, not per frame)
float angularIncrement = verticalSpeed * Input.GetAxis("Mouse Y") * Time.deltaTime;

// Get the current rotation angles.
Vector3 eulerAngles = transform.localEulerAngles;

// Returned angles are in the range 0...360. Map that back to -180...180 for convenience.
if(eulerAngles.x > 180f)
    eulerAngles.x -= 360f;

// Increment the pitch angle, respecting the clamped range.
eulerAngles.x = Mathf.Clamp(eulerAngles.x - angularIncrement, -90f, 90f);

// Orient to match the new angles.
transform.localEulerAngles = eulerAngles;
\$\endgroup\$
  • \$\begingroup\$ Thank you for explaining it works \$\endgroup\$ – NADER LABBAD Aug 15 at 16:37
  • \$\begingroup\$ I think there's something wrong i have try min 0 max 70 This gives me from feet to half screen 0 but if i put min to -30 and up Nothing happens \$\endgroup\$ – NADER LABBAD Aug 15 at 17:00
  • \$\begingroup\$ This is one of the hazards of Euler angles. The engine has to wrap them somewhere - here it picked to wrap the x angle to lie in 0...360, but we want it in -180...180 - add the if line shown above to map it in a more convenient range. \$\endgroup\$ – DMGregory Aug 15 at 17:18
  • \$\begingroup\$ Thanks you Again \$\endgroup\$ – NADER LABBAD Aug 15 at 17:26
0
\$\begingroup\$

You can use if condition such as:

float v = verticalSpeed * Input.GetAxis("Mouse Y");
if (v > 100)  then //100 your limit
    {v= 50}
else 
    {v=30}

or you can use math.sign function which gives you +1,0,-1

if the result for your case v > 0 it returns +1

if it is zero than 0

if it is negative then it is -1

you can limit your camera by that way.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.