• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Sept · Oct 24, 2015 at 04:28 PM · rotationfpsclamplimit

Limit vertical rotation of Camera

Hello, I am new at Unity and C# and I am trying to create a basic FPS controller w/ a limited rotation about the x-axis (vertical rotation).

     public float mouseSensitivity = 3.0f; //Mouse sensitivity.
     public float viewRange = 60.0f;
     float rotVertical = 0;
     
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         //Camera
 
                 float rotHorizontal = Input.GetAxisRaw("Mouse X") * mouseSensitivity;
 
                 transform.Rotate ( 0, rotHorizontal, 0);
 
 
                 rotVertical = Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
 
                 rotVertical = Mathf.Clamp ( rotVertical, -viewRange, viewRange);
 
                 Camera.main.transform.localRotation *= Quaternion.Euler( -rotVertical, 0, 0);
 
 

However, I am still able to rotate 360 degrees vertically. Any help would be appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by MrMeows · Oct 25, 2015 at 04:56 AM

Try clamping it after you apply the rotation.

Camera.main.transform.localEulerAngles = new Vector3( Mathf.Clamp( Camera.main.transform.localEulerAngles.x, -viewRange, viewRange), 0, 0);
By the way, Camera.main is a rather costly operation in terms of framerate. I recommend you use the following.
Transform cameraTransform;
void Start () {
cameraTransform = Camera.main.transform;
}
Then, replace all other instances of Camera.main.transform with cameraTransform. Ctrl+H might be helpful.
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sept · Oct 25, 2015 at 01:12 PM 0
Share

$$anonymous$$uch thanks! I have since replaced Camera.main.transform with cameraTransform. The clamp seems to work for the lower angle ( when i tilt the camera down to face the floor), however, it doesn't work when i tilt the camera up towards the sky. It will not go past a certain angle but ins$$anonymous$$d stutter at a smaller angle (which I'm sure is not the max value for my viewRange).

I replaced

 Camera.main.transform.localEulerAngles = new Vector3( $$anonymous$$athf.Clamp( Camera.main.transform.localEulerAngles.x, -viewRange, viewRange), 0, 0);

with

         Camera.main.transform.localEulerAngles = new Vector3( $$anonymous$$athf.Clamp( Camera.main.transform.localEulerAngles.x, -60, 60), 0, 0);

and the same problem occurs, the lower angle clamp works fine but now tilting upwards past a certain angle will reset the camera to face the floor.

Would you have any idea on how to solve this?

avatar image MrMeows Sept · Oct 25, 2015 at 05:05 PM 0
Share

I have never used $$anonymous$$athf.Clamp, so I do not quite know how it works. I would try using if statements.

if(cameraTransform.localEulerAngles.x > viewRange)
{
cameraTransform.localEulerAngles = new Vector3( viewRange, 0, 0);
} else
{
if(cameraTransform.localEulerAngles.x < -viewRange)
{
cameraTransform.localEulerAngles = new Vector3( -viewRange, 0, 0);
}
}
avatar image Sept MrMeows · Oct 26, 2015 at 12:04 AM 0
Share

Ah, the if statement seems to work out fine! Thank you very much for your help!

Show more comments
Show more comments
avatar image
2

Answer by isopoly23 · Sep 17, 2018 at 06:04 PM

     public float SpeedH = 10f;
     public float SpeedV = 10f;
 
     private float yaw = 0f;
     private float pitch = 0f;
     private float minPitch = -30f;
     private float maxPitch = 60f;
     
     void Update()
     {
         CameraRotate();
     }
 
     void CameraRotate() {
         yaw += Input.GetAxis("Mouse X") * SpeedH;
         pitch -= Input.GetAxis("Mouse Y") * SpeedV;
         pitch = Mathf.Clamp(pitch, minPitch, maxPitch); 
         transform.eulerAngles = new Vector3(pitch, yaw, 0f);
         
     }
 
    
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrdav30 · Nov 14, 2018 at 11:20 PM 0
Share

This is a good method if you allow rotation on the y-axis as well. The accepted answer will always resets it back to 0. This also gives better control over the rotation speed.

avatar image
0

Answer by ecreators · Apr 15, 2018 at 08:47 AM

I needed the logic to camera tilting angle (axis x) in inspector.

I use this:

         // ..  in void Update() in an if statement, only when left mouse is pressed if( Input.GetMouseButton(0)) { ... }
         
         // vertically mouse y delta to last frame
         var fY   = Input.GetAxis(InputAxis.MOUSE_Y);
         var v    = gcProfile.CameraTurnSpeedVertically * fY;
         
         // make rotation
         var rotation = new Vector3(-v, 0, 0);
         var tt       = target.transform;
         tt.Rotate(rotation, Space.Self);
         
         // limits for angle in tilt x axis, as seen in inspector
         const int bottom = 45;
         const int top    = -90;
         
         // convert transform to values, as seen in inspector
         var eulerTiltX = TransformUtils.GetInspectorRotation(tt).x;
         Debug.Log("local euler tilt (x): " + eulerTiltX + " <- " + fY);
         
         // x (inspector angle for tilt angle)
         
         // as long as try to rotate camera vertically in fY direction and the angle in inspector reaches limit ...
         
         if (eulerTiltX > bottom) //  && fY < 0 -- not really nessessary
         {
                  // then: set inspector value to bottom boundary
             TransformUtils.SetInspectorRotation(tt, new Vector3(bottom, 0, 0));
         }
         else if (eulerTiltX < top) // && fY > 0 -- not really nessessary
         {
             TransformUtils.SetInspectorRotation(tt, new Vector3(top, 0, 0));
         }






Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ammdhillon · Nov 10, 2020 at 01:35 PM

That's what I did and clamping works perfectly.

 private float cameraPanMax = 20f; // 20 degree
 private float cameraPanMin = 340f; // -20 degree (360 - 20)

 // Update() starts here

 float moveHorizontal = CrossPlatformInputManager.GetAxisRaw("Horizontal");
 float moveVertical = CrossPlatformInputManager.GetAxisRaw("Vertical");
 
 if (moveVertical != 0) 
             {
                 Vector3 rotateDir = new Vector3(-moveVertical, 0f, 0f);
                 CameraManager.activeCam.Rotate(rotateDir * rotSpeed * Time.deltaTime);
 
                 float currentAngle = CameraManager.activeCam.localEulerAngles.x;
 
                 bool isHigherThanRange = currentAngle > cameraPanMax && currentAngle < 50f;
                 bool isLowerThanRange = currentAngle < cameraPanMin && currentAngle > 300f;
 
                 if (isHigherThanRange)
                 {
                     CameraManager.activeCam.localEulerAngles = new Vector3(Mathf.Clamp(currentAngle, 0f, cameraPanMax), 0, 0);
                 }
 
                 if (isLowerThanRange) 
                 {
                     CameraManager.activeCam.localEulerAngles = new Vector3(Mathf.Clamp(currentAngle, cameraPanMin, cameraPanMin), 0, 0);
                 }
             }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to clamp transform.Rotate values? 0 Answers

How to rotate camera in FPS with joystick 4 Answers

Clamp Lever Rotation 0 Answers

Rotate camera upward. 0 Answers

Why is this code in the script not stopping the rotation of the Gameobject? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges