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 Cartman · Sep 05, 2012 at 07:06 PM · camerarotationlimit

Camera Limit Rotations Problem

I use this code to limit the rotation on the X and Y axis of my camera.

 public void LimitRotation()
     {
         Quaternion axisRotation = Quaternion.AngleAxis(t.localRotation.eulerAngles.x, Vector3.right);
         float angleFromMin = Quaternion.Angle( axisRotation, lowerRotationLimit );
         float angleFromMax = Quaternion.Angle( axisRotation, upperRotationLimit );
             
         if ( angleFromMin > rotationRange || angleFromMax > rotationRange )
         {
             Vector3 euler = t.localRotation.eulerAngles;
             euler.x = (angleFromMin > angleFromMax) ? upperRotationLimit.eulerAngles.x : lowerRotationLimit.eulerAngles.x;
             t.localEulerAngles = euler;        
         }
     }

But if i move the camera up and down really fast the camera freaks out and stays in a weird rotated position. Full Script:

 public class CameraPivot : MonoBehaviour
 {
     public Transform target; //Target to follow
     public Vector3 offset; //Offset from target
     public float horizontalRotationSpeed; //Horizontal rotation speed of the camera pivot
     public float verticalRotationSpeed; //Vertical rotation speed of the camera pivot
     public Vector2 rotationLimit; //Limit for the vertical rotation of the camera. X is higer limit, Y is lower limit.
     public bool invertY; //Invert vertical rotation
     
     //Rotation values used for limiting rotation of the camera pivot
     private Quaternion upperRotationLimit;
     private Quaternion lowerRotationLimit;
     private float rotationRange;
     private Transform t; //The target the camera is looking at
     
     public void Awake()
     {
         t = transform; //Cache transform component
         //Calculate final upper and lower rotation limits and the rotation range
         lowerRotationLimit = t.localRotation * Quaternion.AngleAxis( rotationLimit.y, Vector3.right );
         upperRotationLimit = t.localRotation * Quaternion.AngleAxis( rotationLimit.x, Vector3.right );
         rotationRange = rotationLimit.x - rotationLimit.y;
     }
     
     public void LateUpdate()
     {
         //Follow player
         t.position = target.position + offset;
         //Update rotation
         t.Rotate(0, Input.GetAxis("Mouse X") * horizontalRotationSpeed * Time.deltaTime, 0, Space.World);
         t.Rotate(((invertY) ? -1 : 1) * Input.GetAxis("Mouse Y") * verticalRotationSpeed * Time.deltaTime, 0, 0);
         //Limit rotation
         LimitRotation();
     }
     
     public void LimitRotation()
     {
         Quaternion axisRotation = Quaternion.AngleAxis(t.localRotation.eulerAngles.x, Vector3.right);
         float angleFromMin = Quaternion.Angle( axisRotation, lowerRotationLimit );
         float angleFromMax = Quaternion.Angle( axisRotation, upperRotationLimit );
             
         if ( angleFromMin > rotationRange || angleFromMax > rotationRange )
         {
             Vector3 euler = t.localRotation.eulerAngles;
             euler.x = (angleFromMin > angleFromMax) ? upperRotationLimit.eulerAngles.x : lowerRotationLimit.eulerAngles.x;
             t.localEulerAngles = euler;        
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Sep 05, 2012 at 07:12 PM

http://answers.unity3d.com/questions/297726/quaternioneulernew-vector39500eulerangles-new-vect.html

public class CameraPivot : MonoBehaviour { public Transform target; //Target to follow public Vector3 offset; //Offset from target public float horizontalRotationSpeed; //Horizontal rotation speed of the camera pivot public float verticalRotationSpeed; //Vertical rotation speed of the camera pivot public Vector2 rotationLimit; //Limit for the vertical rotation of the camera. X is higer limit, Y is lower limit. public bool invertY; //Invert vertical rotation

private Transform _transform; private Quaternion initialRotation; private float currentXrotation = 0f; private float currentYrotation = 0f;

void Awake() { _transform = transform; //Cache transform component }

void Start() { //store initial rotation initialRotation = transform.rotation; }

void LateUpdate() { //Follow player _transform.position = target.position + offset;

//read mouse movements currentXrotation += Input.GetAxis("Mouse Y") horizontalRotationSpeed Time.deltaTime; currentYrotation += ((invertY) ? -1 : 1) Input.GetAxis("Mouse X") verticalRotationSpeed * Time.deltaTime;

//limit rotations currentYrotation = Mathf.Repeat(currentYrotation, 360f); currentXrotation = Mathf.Clamp(currentXrotation, rotationLimit.x, rotationLimit.y);

//rotate _transform.rotation = initialRotation * Quaternion.Euler(currentXrotation, currentYrotation, 0f); } }

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 Cartman · Sep 06, 2012 at 11:39 AM 0
Share

Can't get it to work. Tryed this:

 public void LimitRotation()
     {
         Quaternion axisRotation = Quaternion.AngleAxis(t.localRotation.x, Vector3.right);
         float angleFrom$$anonymous$$in = Quaternion.Angle( axisRotation, lowerRotationLimit );
         float angleFrom$$anonymous$$ax = Quaternion.Angle( axisRotation, upperRotationLimit );
             
         if ( angleFrom$$anonymous$$in > rotationRange || angleFrom$$anonymous$$ax > rotationRange )
         {
             Quaternion euler = t.localRotation;
             euler.x = (angleFrom$$anonymous$$in > angleFrom$$anonymous$$ax) ? upperRotationLimit.x : lowerRotationLimit.x;
             t.localRotation = euler;
         }
     }
avatar image ScroodgeM · Sep 06, 2012 at 08:12 PM 0
Share

check answer again

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

8 People are following this question.

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

Related Questions

Help With Arrow Camera Script 2 Answers

Controlling camera rotation limits. 1 Answer

Rotation Constraint doesn't work properly 1 Answer

Problem when applying forces to a ball 1 Answer

"Free" rotation about a sphere 0 Answers


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