Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 _-puppytails-_ · May 14, 2020 at 12:41 AM · mathf.clamp

I am trying to clamp a camera, however the camera "jumps"

I have no ideas at this point as to what can be the issue. I've tried to get help multiple times but it never really works. What is at fault here?

   rotateX += Input.GetAxis("Mouse X") * s.YSens * Time.deltaTime;
   rotateY += -Input.GetAxis("Mouse Y") * s.XSens * Time.deltaTime;
   rotateY = Mathf.Clamp(rotateY, -90 - Camera.transform.localRotation.eulerAngles.y, 90 - Camera.transform.localRotation.eulerAngles.y);
   
   Quaternion qq = new Quaternion();
   qq = Quaternion.AngleAxis(rotateY, Vector3.right);
   Camera.transform.localRotation = Camera.transform.localRotation * qq;
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
Best Answer

Answer by icehex · May 14, 2020 at 05:00 AM

Hi, I figured out what the issue was - when converting rotateY into a Quaternion rotation, if rotateY is between 0 and 90, no issue. If it falls just below zero say at -0.1 degrees, the new rotation is at 359.9 degrees. So that's the cause of the jumpy behavior, since 359.9 would get clamped to 90, which is a jarring change and incorrect.


Using clamp is a bit un-ideal because with it you don't know whether the clamping was required or not. So if it was required, you won't know to not rotate your camera this frame. I would suggest using some if statements which are very efficient anyway. The below code worked in Unity properly. Keep in mind though since you track rotateY as cumulative, the mouse rotations are accelerated/decelerated. If you want a fixed rotation rate, just check if rotateY is positive or negative, and apply a fixed amount of rotation to rotX - i.e. add or subtract s.XSens to/from rotX.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CamClamp : MonoBehaviour
 {
     float rotateX;
     float rotateY;
     float rotX;
 
     public Camera Camera;
 
     void Update()
     {
         rotateX += Input.GetAxis("Mouse X") * 5 * Time.deltaTime;
         rotateY += -Input.GetAxis("Mouse Y") * 5 * Time.deltaTime;
 
         rotX = Camera.transform.localRotation.eulerAngles.x;
 
         // We do this so that now the angle rotX is guaranteed to be somewhere between -90 to 90
         if (rotX >= 270)
         {
             rotX -= 360;
         }
 
         if (rotX > 90)
         {
             rotX = 90;
         }
         else if (rotX < -90)
         {
             rotX = -90;
         }
         else
         {
             rotX += rotateY;
         }
 
         Camera.transform.localRotation = Quaternion.AngleAxis(rotX, Vector3.right);
 
     }
 }
 

Last note on convention - when you do rotations, you rotate around axes. I understand your rotateY variable moves the screen up and down along the Y axis, but it is equivalently a rotation around the X axis.

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 _-puppytails-_ · May 14, 2020 at 09:28 PM 0
Share

Oh, I see! Thank you very much for your help and taking the time out to help me on this issue.

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

126 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

Related Questions

Limiting a GameObject Rotation 1 Answer

Camera shaking and barely moving after adding a clamp to x rotation? 2 Answers

Flicker while detecting edge of screen with Mathf.clamp 0 Answers

rotation won't rotate properly 1 Answer

Clamping movement range on X-axis 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