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 Herooik · Feb 26, 2021 at 07:53 AM · rotationcamera rotatekeyboardverticalclamped rotation

Limit camera vertical rotation with keyboard

Hello. I want to rotate the camera around the interacted object with keys. My horizontal rotate works perfectly. But I don't know how to handle vertical rotation. I got this code from this question. I also want to clamp this vertical rotation just like in horizontal. I already tried to paste the code from horizontal to vertical and just change the currentVector and axis on RotateAround but this was working only to target objects in specified positions on scene.

 private void RotateHorizontal()
     {
         _horizontalRotateDegrees = 0;
         
         var value = horizontalRotate.action.ReadValue<float>();
         
         if(horizontalRotate.action.ReadValue<float>() < 0)
         {
             _horizontalRotateDegrees -= rotateSensitivity.Value * value * Time.deltaTime;
         }
         if(horizontalRotate.action.ReadValue<float>() > 0)
         {
             _horizontalRotateDegrees -= rotateSensitivity.Value * value * Time.deltaTime;
         }
         
         var currentVector = playerCamera.transform.position - _targetTransformPos;
         currentVector.y = 0;
         
         var angleBetween = Vector3.Angle(_initialVector, currentVector) *
                              (Vector3.Cross(_initialVector, currentVector).y > 0 ? 1 : -1);
         var newAngle = Mathf.Clamp(angleBetween + _horizontalRotateDegrees, -angleHorizontal, angleHorizontal);
 
         _horizontalRotateDegrees = newAngle - angleBetween;
         
         playerCamera.transform.RotateAround(_targetTransformPos, Vector3.up, _horizontalRotateDegrees);
     }


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
0

Answer by Ermiq · Feb 26, 2021 at 10:41 AM

For camera rotation that rotates around player or some other object I prefer to use the following setup for camera that consists of empty GameObjects with the following hierarchy:
1. CamRotationPivotH - a root game object, and it considered to rotate around it's Y axis only (rotates in World space).
2. CamRotationPivotV - a child of the CamRotationPivotH, it rotates around its X axis only (rotates in Local space).
3. Camera component is attached to a game object that is a child of the CamRotationPivotV. It's local rotation could be rotated anyhow depending on different effects and states of the player, like recoil, damage hit effect, speed burst shake effect, and it won't affect the rotation from player inputs.

 public bool clampVerticalRotation;
 public float MinimumX; //min angle for vertical rotation
 public float MaximumX; //max angle for vertical rotation
 Quaternion TargetRotationH;
 Quaternion TargetRotationV;
 
 public void RotateCamera(float horizontal, float vertical)
 {
     // Get current rotations
     TargetRotationV = CamRotationPivotV.localRotation;
     TargetRotationH = CamRotationPivotH.rotation;
     // Add desired horizontal and vertical angles to the current rotations
     TargetRotationV *= Quaternion.Euler(-vertical * Time.deltaTime, 0f, 0f);
     TargetRotationH *= Quaternion.Euler(0f, horizontal * Time.deltaTime, 0f);
     // Clamp vertical
     if (clampVerticalRotation)
         TargetRotationV = ClampRotationAroundXAxis(TargetRotationV);
     // Apply rotations to the camera objects
     CamRotationPivotV.localRotation = TargetRotationV;
     CamRotationPivotH.rotation = TargetRotationH;
 }

 // Here is some Quaternion magic that I copy/pasted from somewhere:
 Quaternion ClampRotationAroundXAxis(Quaternion q)
 {
     q.x /= q.w;
     q.y /= q.w;
     q.z /= q.w;
     q.w = 1.0f;
     float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
     angleX = Mathf.Clamp(angleX, MinimumX, MaximumX);
     q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
     return q;
 }

To make it rotate around your object, set the CameraRotationPivotH.position to the object's position, and set the Camera.transform.localPosition.z to something like -5, for example. And send desired rotation angles to the RotateCamera() method.

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Making object Clamp to Look Rotation of Camera 1 Answer

Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers

Make camera move and look at an object over time 0 Answers

How to make the camera follow the player while still being able to be rotated? 1 Answer

How to clamp rotation in degrees in relation to another GameObject 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