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 jessee03 · Dec 12, 2020 at 07:56 PM · cameracamera-movement

Issue with clamping camera

I'm trying to clamp the angle of my camera but clamping is not working

     public void MoveCamera()
     {
             float pitch = Input.GetAxis("Mouse Y") * 100 * Time.deltaTime;
             float yaw = Input.GetAxis("Mouse X") * 100 * Time.deltaTime;
             ClampAngle(pitch);
 
             Camera.main.transform.Rotate(-pitch, 0, 0, Space.Self);
             Camera.main.transform.Rotate(0, yaw, 0, Space.World);
     }

     void ClampAngle(float angle)
     {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
 
         yDeg = Mathf.Clamp(angle, -60, 80);
     }
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 tategarringer · Dec 12, 2020 at 08:53 PM

Firstly, looks like you're setting variable yDeg to the clamped value of your angle and then never using it. Also, realistically, if you're clamping the angle value between -60 and 80, there's no reason to have the logic:


  if (angle < -360)
      angle += 360;
  if (angle > 360)
      angle -= 360;


The subsequent Mathf.Clamp dictates that even if either scenario occurs, it'll clamp it to -60 or 80 respectively. Secondly, I'm sure you're aware that Input.GetAxis returns a normalized value between -1 and 1, hence your inclusion of a multiplicative factor, but then you go and multiply it by Time.deltaTime, so the scenario where value clamping would be necessary will never occur unless your framerate is INCREDIBLY slow.


You could ditch the ClampAngle() method entirely and sum it up in one line:


     float pitch = Mathf.Clamp(Input.GetAxis("Mouse Y") * 100, -60, 80);


And then change your rotation function to:


     Camera.main.transform.Rotate(-pitch * Time.deltaTime, 0, 0, Space.Self);


Tested and working with MoveCamera() called in Update(), this will ensure that your angle value is clamped BEFORE you apply your "angle per frame" calculation utilizing Time.deltaTime.

Comment
Add comment · Show 4 · 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 jessee03 · Dec 12, 2020 at 09:59 PM 0
Share

I tried all the changes but clamping is still not working for some reason

             float pitch = $$anonymous$$athf.Clamp(Input.GetAxis("$$anonymous$$ouse Y") * 100, -60, 80);
             float yaw = Input.GetAxis("$$anonymous$$ouse X") * 100 * Time.deltaTime;
 
             Camera.main.transform.Rotate(-pitch * Time.deltaTime, 0, 0, Space.Self);
             Camera.main.transform.Rotate(0, yaw, 0, Space.World);
avatar image tategarringer jessee03 · Dec 12, 2020 at 10:10 PM 0
Share

By "not working" what exactly do you mean? Is it moving beyond the parameters of the $$anonymous$$athf.Clamp function? No change whatsoever? Have you tried to Debug.Log() your pitch value as it's running? It's hard to go off a generic "not working".

avatar image jessee03 tategarringer · Dec 16, 2020 at 07:32 PM 0
Share

I tried to debug but the numbers are all over the place, so I wonder if Space.Self or something else is causing issues. The numbers are exceeding the clamp values though

avatar image TreyH · Dec 16, 2020 at 07:56 PM 0
Share

OP is trying to clamp the angle of their camera, not the magnitude of its rotation, btw. The code here will limit how much that rotation can change each update tick, but not the rotation itself.

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

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

TPS camera in multiplayer... 2 Answers

How do I get my camera to rotate around the y=0 coordinate it's looking at? 0 Answers

Help My movement is broken!! 2 Answers

Camera movement stuttery with interpolate on,Camera movement with interpolate on? 0 Answers

Make camera follow player and align direction 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