Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by jjmontes · May 17, 2014 at 04:41 PM · quaternionclampclamped rotation

Clamp camera quaternion so you can't look up or down more than 90 degrees?

I'm trying to limit my camera movements in order to avoid it "flipping" when you look up or down further than 90 degrees, while keeping the Up vector always pointing up.

I'm currently using the following:

 // Process controls
 var cdir : Vector3 = transform.rotation * Vector3.forward;
 cdir = (Quaternion.AngleAxis(rotX, axisX)) * cdir;
 cdir = (Quaternion.AngleAxis(rotY, Vector3.Cross(cdir, Vector3.up).normalized)) * cdir;   
 transform.rotation = Quaternion.LookRotation(cdir);

I would like to clamp the vertical (axisY) angle to something between -89 and 89 degrees, but I fail to find a proper way to do this. I believe it shall be easier to do this in the quaternion domain, but I don't know how to test for this.

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

3 Replies

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

Answer by Cherno · May 17, 2014 at 10:45 PM

Take a look at this mouselook script.

 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
 
     float rotationY = 0F;
 
     void Update ()
     {
         /*
         if (axes == RotationAxes.MouseXAndY)
         {
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
             
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
         else if (axes == RotationAxes.MouseX)
         {
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         }
         else
         {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
         }
         */
 
         if(Screen.lockCursor == true) {
             if (axes == RotationAxes.MouseXAndY) {
                 float rotationX = new float();
                 
                 if(transform.parent != null && transform.parent.CompareTag("Player") == true) {
                     rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                     
                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                     
                     transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
                     transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
                     
                 }
                 else if(transform.parent != null) {
                     rotationX = transform.parent.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                     
                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                     
                     transform.parent.transform.localEulerAngles = new Vector3(0, rotationX, 0);
                     transform.localEulerAngles = new Vector3(-rotationY, 0, 0);
                 }
                 /*
                 else {
                     rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
                     
                     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                     rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                     
                     transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
                 }
                 */
             }
             else if (axes == RotationAxes.MouseX) {
                 if(transform.parent != null) {
                     transform.parent.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
                 }
                 else {
                     transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
                 }
             }
             else {
                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
                 
                 transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 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
1

Answer by loondas · May 17, 2014 at 11:12 PM

I have been using this bit of condensed code for looking. This is also in C#

 `public float updownrange = 60.0f;
 float verticalrotation = 0;
 public float mousesensitivity = 4.0f;
 
 void Update ()
         {
 verticalrotation -= Input.GetAxis ("Mouse Y") * mousesensitivity;
 
     verticalrotation = Mathf.Clamp (verticalrotation, -updownrange, updownrange);

                 Camera.main.transform.localRotation = Quaternion.Euler (verticalrotation, 0, 0)}`

This assumes the camera is a child of your character, and rotates the camera rather than your actual character. So the character collider doesn't actually move.

Comment
Add comment · Show 2 · 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 shaq_ · Apr 15, 2016 at 05:00 AM 0
Share

what if i have multiple camera and i want to be able to control it whenever i switch the camera? currently my camera2 is facing 180deg but whenever i play the game, the rotation of my camera2 will set to 0,0,0 ins$$anonymous$$d of 0,180,0. can anyone help me with this?

avatar image exvalid shaq_ · Feb 20, 2018 at 06:07 PM 0
Share

u need to set up two seperate $$anonymous$$ max angles under two conditions ie

public bool camera1 = false ; public bool camera2 = false ;

public camera = //find your camera ; public camera2 = //find your camera2 ;

//Starts

{

if ( camera1.enabled == true )

Camera1= true

}

{

if ( camera1.enabled == false )

Camera1= false ;

}

//ends

//Starts

{

if ( camera2.enabled == true )

Camera2= true

}

{

if ( camera2.enabled == false )

Camera2= false ;

}

//ends

//Starts

{

if (camera1 == true)

verticalrotation = $$anonymous$$athf.Clamp (verticalrotation, -updownrange, updownrange);

}

//Ends

//Starts

{

if (camera2 == true)

verticalrotation = $$anonymous$$athf.Clamp (verticalrotation, -updownrange2, updownrange2); //here you would change the clamp to suit your camera 2. ins$$anonymous$$d. by using another $$anonymous$$ max variable/float.

}

Cheers hope this helps any1.

Exvalid

//Ends

avatar image
0

Answer by igor-pakushin · Sep 07, 2020 at 11:45 AM

 using UnityEngine;
 using System;
 
 public class Orbit : MonoBehaviour
 {
     public Transform player;
     public float turnSpeed = 4.0f;
     public float offset = -13;
     public Vector2 cameraRotation = new Vector2(0, 20);
 
     void Start()
     {
         UpdateTransform();
     }
 
     void Update()
     {
         if (IsMouseDown())
         {
             UpdateCameraRotation();
         }
 
         UpdateTransform();
     }
 
     private void UpdateCameraRotation()
     {
         cameraRotation.x += Input.GetAxis("Mouse X") * turnSpeed;
         cameraRotation.y -= Input.GetAxis("Mouse Y") * turnSpeed;
         cameraRotation.x = Mathf.Clamp(cameraRotation.x, -180, 180);
         cameraRotation.y = Mathf.Clamp(cameraRotation.y, -89, 89);
     }
 
     private void UpdateTransform()
     {
         var xQuaternion = Quaternion.AngleAxis(cameraRotation.x, Vector3.up);
         var yQuaternion = Quaternion.AngleAxis(cameraRotation.y, Vector3.right);
         var combinedQuaternion = xQuaternion * yQuaternion;
 
         transform.position = player.position + combinedQuaternion * Vector3.forward * offset;
         transform.LookAt(player.position);
     }
 
     private bool IsMouseDown()
     {
         return Input.GetMouseButton(0);
     }
 }
 
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 igor-pakushin · Sep 07, 2020 at 11:47 AM 0
Share

Put this C# script on your camera, assign player property. offset is how far an object is going to be offset from camera, cameraRotation is the angles will the camera be looking at your object

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.

Follow this Question

Answers Answers and Comments

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mathf.Clamp is not clamping my minimum value 1 Answer

How to Clamp a Quaternion Camera Rotation? 0 Answers

Clamping the camera X axis when not using the mouse axis? C# 0 Answers

Rotation - Simple Question 0 Answers

How to clamp character rotation on Y and X axis? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges