• 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 Glolut · Jan 22 at 10:48 AM · rotationrotaterotate objecteulerangles

How to limit rotation of an object when pressing a key?

Currently, my gameObject is rotated up and down by pressing a key. This is my code:

 switch (cannon){
     case 0: break;
     //press W
     case 1:  Cannon.transform.localEulerAngles -= new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
             // print(Cannon.transform.rotation);  
             ;break;
     //press S
     case 2: Cannon.transform.localEulerAngles += new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
             ;break;
 }

I want when the user keeps pressing W, the Cannon rotate up but Rotation X in the inspectator capped at -25. And capped at 25 when pressing S(rotate down). alt text

new.png (10.5 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by knobblez · Jan 22 at 10:59 AM

You just need to get the current rotation and do a IF STATEMENT to check if it is acceptable to rotate more.

    switch (cannon)
             {
                 case 0:
                     {
                         break;
                     }
                 //press W
                 case 1:
                     {
                         if (Cannon.transform.rotation.x > -25f)
                         {
                             Cannon.transform.localEulerAngles -= new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
                             // print(Cannon.transform.rotation);
                         }
                         break;
                     }
                 //press S
                 case 2:
                     {
                         if (Cannon.transform.rotation.x < 25f)
                         {
                             Cannon.transform.localEulerAngles += new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
                         }
                         break;
                     }
             }
 
 
Comment
Add comment · Show 7 · 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 Captain_Pineapple · Jan 22 at 11:21 AM 0
Share

as a sidenote: This is not precise. Not at all. Furthermore it gets worse the smaller the framerate.


imagine you have an angle of -24.95f and thus decide that you can still rotate. Your rotationspeed is 2f and you get a laggy frame that is like 1 second long. Then you have rotated to -26.95f.


A better check would be if the current angle + the change is larger than the max value. If it is then rotate to the max angle. If it isn't you can normally apply the rotation.

avatar image knobblez Captain_Pineapple · Jan 22 at 12:14 PM 0
Share

I really meant <= 23f but I figured he'd get the gist. You're right though. Would be safer to check currentAngle + changeAmount. And probably have it allow the difference if below 25f. That way it's consistent and won't sometimes go 24.98 and sometimes 25.

avatar image Captain_Pineapple knobblez · Jan 22 at 12:32 PM 0
Share

why <= 23f? that does not make any sense.

avatar image Glolut · Jan 22 at 11:23 AM 0
Share

Sorry man, I thought rotation.x refers to X rotation in the inspectatoralt text

What I really want is transform.localEulerAngles capped at -25 and 25. But when the object rotate up, it show in the image that localEulerAngles.x = -25. But when I print it in console it give me 334.2495

new.png (10.5 kB)
avatar image knobblez Glolut · Jan 22 at 12:33 PM 0
Share

Just put localEulerAngles instead of rotation

avatar image Captain_Pineapple Glolut · Jan 22 at 12:35 PM 0
Share

this really should be the first thing unity teaches about itself...

it comes up so often...


Unity will not display negative angles. So when you rotate something and the angle would be negative unity just adds 360. Which is annoying but makes sense if you think a bit further.


So what can you do?

Add another if statement.

    pseude code:
    angle = transform.rotation.x
    if angle > 180
         angle -= 360
    then do the check you currently do.

not really that nice but should do the trick

avatar image Glolut Captain_Pineapple · Jan 22 at 01:09 PM 0
Share

Thanks for that. I got your idea and It's work!

avatar image
0

Answer by bhavinbhai2707 · Jan 22 at 02:21 PM

There is a very simple way to clamp the rotation of the Gameobject. You need to use Mathf.Clamp01 in your situation. The above-provided solution by Knoblez is not precise and has a chance of failure due to FPS issues.

Mathf.Clamp01 is used to limit a value between two numbers.

         //This will clamp the rotation of the object between minRotation and maxRotation. 
          float minRotation = 1f;
          float maxRotation = 90f;
          Vector3 currentRotation = transform.localRotation.eulerAngles;
          currentRotation.z = Mathf.Clamp01(currentRotation.z, minRotation, maxRotation);
          transform.localRotation = Quaternion.Euler(currentRotation);

Simply add this piece of code after you change the rotation of the object.

Comment
Add comment · Show 5 · 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 Glolut · Jan 22 at 02:32 PM 0
Share

I just read the docs that u provide and it shows that Mathf.Clamp01(float value) only take 1 argument.

avatar image bhavinbhai2707 Glolut · Jan 22 at 02:36 PM 0
Share

I provided you with a code snippet on how to use it. If you suppose just need to clamp one axis, like suppose z? then use the code above, or if you need to clamp all the values then simply clamp all three axis as

  //This will clamp the rotation of the object between $$anonymous$$Rotation and maxRotation. 
           float $$anonymous$$Rotation = 1f;
           float maxRotation = 90f;
           Vector3 currentRotation = transform.localRotation.eulerAngles;
          currentRotation.x = Mathf.Clamp01(currentRotation.x, $$anonymous$$Rotation, maxRotation);
          currentRotation.y = Mathf.Clamp01(currentRotation.y, $$anonymous$$Rotation, maxRotation);
           currentRotation.z = Mathf.Clamp01(currentRotation.z, $$anonymous$$Rotation, maxRotation);
           transform.localRotation = Quaternion.Euler(currentRotation);
avatar image Glolut bhavinbhai2707 · Jan 22 at 02:49 PM 0
Share

I think you are suggesting me to use Math.Clamp() instead of Math.Clamp01(). Because it only clamps value between 0 and 1 and returns value. But I agree that ur solution solve the Knoblez's answer problem. However, eulerAngles does not take negative value as show in the Inspectator image I provided. When I print it on console it show me 337, instead of -23. So we need to convert 337 to -23 first then we can use Math.Clamp() to limit the rotation

Show more comments

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

182 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

Related Questions

When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers

How do I get the rotation to work past the sensitivity?, 1 Answer

How to make a continous swivel? Rotation 0 Answers

How to rotate an object around another 60 degrees with a keypress? 0 Answers

Proper way to rotate an object? 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