Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by Blamanos · Jan 02, 2018 at 01:56 PM · quaternioneuleranglesclamped rotation

Problem with negative values when using mathf.clamp to limit rotation

I have encountered a problem in my code. The purpose of this script is to make the main camera rotate on the x and y axis with help of wasd and the mouse when it is close to the edges of the screen.

The problem is when I try to implement limits to the rotation with mathf.clamp. I realize that mathf.clamp doesn't like negative values, but I don't know how to solve it. So when I'm trying to limit a cameras rotation between -15 and 35 on the x axis and between -175 and -105 on the y axis. On the x axis, the rotation stops at 35, but as soon as I go over the 0 value on the x axis, it snaps to 35. As for the y axis, it is constantly locked to -105.

I get that Euler doesn't have negative values, but I have yet to find a workaround for this.

Anyway, here's the code:

 using UnityEngine;
 
 public class CameraController : MonoBehaviour {
 
     public float rotSpeed = 20f;
     public float rotBorderThickness = 40f;
 
     void Update () {
 
         Quaternion qRot = Camera.main.transform.rotation;
         Vector3 rot = qRot.eulerAngles;
 
         if ((Input.GetKey("w") || Input.mousePosition.y >= Screen.height - rotBorderThickness))
         {
                 rot.x -= rotSpeed * Time.deltaTime;
         }
         if (Input.GetKey("s") || Input.mousePosition.y <= rotBorderThickness)
         {
                 rot.x += rotSpeed * Time.deltaTime;
         }
         if (Input.GetKey("a") || Input.mousePosition.x <= rotBorderThickness)
         {
             rot.y -= rotSpeed * Time.deltaTime;
         }
         if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - rotBorderThickness)
         {
             rot.y += rotSpeed * Time.deltaTime;
         }
         //Here's the problem
         rot.y = Mathf.Clamp(rot.y, -175, -105);
         rot.x = Mathf.Clamp(rot.x, -15, 35);
 
         Quaternion newRot = Quaternion.Euler(rot);
         transform.rotation = newRot;
     }
 }
 
Comment
Add comment · Show 4
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 Blamanos · Jan 02, 2018 at 02:06 PM 0
Share

@Lysander saw your comment so I created a new post

avatar image Lysander Blamanos · Jan 02, 2018 at 03:02 PM 0
Share

Please convert this to a comment ins$$anonymous$$d of an answer- if people see that an answer exists already, sometimes they won't bother checking the question.

In regards to your problem here, $$anonymous$$athf.Clamp has no problem with negative values. Internally, it's literally just "if value is less than $$anonymous$$, value equals $$anonymous$$- if value is greater than max, value is max"- there's nothing to go wrong there. I suspect the problem is the assumption that eulerangles correlates to the values you see in the inspector. Ins$$anonymous$$d of clamping them, try just debug logging the values to the console once a second and see what that looks like.

avatar image Blamanos Lysander · Jan 03, 2018 at 10:51 AM 0
Share

Well, the rot.x values in the log showed as 345 ins$$anonymous$$d of -15, which is logical because 360 -15 = 345, but If I use 345 in $$anonymous$$athf.clamp it takes the opposite angle as to what I want, where 345 is the largest value and 35 the lowest, when it should be the other way around.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

121 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

Related Questions

Any simple ways of keeping track of simple rotation? 2 Answers

Rotate clockwise/counterclockwise using Quaternion.Slerp() 0 Answers

Quaternion.Euler doesn't work right? 2 Answers

MathF clamp look toward mouse pointer not working correctly 0 Answers

What's wrong with this Quaternion rotation? 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