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 /
avatar image
0
Question by Poranny · Jan 16, 2018 at 07:20 PM · rotationcontrolstop

Rotation control (C#)

Hello everyone! I have an object which I rotate by mouse movement. The object's rotation (axis x) increases when mouse moves up and decreases when the mouse moves down (I use Input.GetAxis ("Mouse Y")). The problem I have is how to stop the rotation when it reaches a certain point, for example 200 degrees.

 if (rotation.x < 200)  {
       object.transform.Rotate (mouseY, 0, 0);
 }
    
 else if (rotation.x >= 200) {
       if (mouseY < 0) {
               object.transform.Rotate (mouseY, 0, 0);
       }
 }

With this code the value increases and decreases normally when it's below 200, but when it comes above it can only be decreased. The problem is, when I move mouse fast enough the value stops at, for example, 205, instead of 200. My question is, how to make the rotating stop exactly at number 200? I've already tried many ways, such as slowing down the mouse movement, making the object rotate back or forcing the rotation to be exactly 200 when the value is over 200, etc. but these are only workarounds, which do not work perfectly and create a lot of bugs.

I want it to look like that: rotation.x = 190 (mouse move up) rotation.x = 200 (mouse move up) rotation.x = 200 (mouse move down) rotation.x = 190

Feel free to ask for details, I really appreciate any help!

Comment
Add comment · Show 1
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 Legend_Bacon · Jan 16, 2018 at 07:46 PM 1
Share

Could THIS help you?

Cheers!

~LegendBacon

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by alelavoie · Jan 17, 2018 at 04:50 AM

You could avoid going over 200 degrees by checking if the "rotation to apply" would exceed the 200 degree limit and act accordingly.

To take your code example, it would look something like that:

 if (rotation.x < 200)  {
      if (mouseY > (200 - rotation.x)) {
         object.transform.Rotate (200 - rotation.x, 0, 0);
     } else {
         object.transform.Rotate (mouseY, 0, 0);
     }       
      
  }
     
  else if (rotation.x >= 200) {
        if (mouseY < 0) {
                object.transform.Rotate (mouseY, 0, 0);
        }
  }

But this code would have a strange behaviour to my opinion. It would always rotate the object when you move the mouse down but would rotate only if angle < 200 when you move the mouse up. Maybe it fits in your case anyway. Hope that helps.

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 Poranny · Jan 17, 2018 at 02:09 PM 0
Share

Big thanks for your and @Legend_Bacon replies, your suggestions helped me to deal with the issue, but it turned out that there were problems muuch deeper in my code. Anyway, finally I did overcome it and everything works as intended :) Once again, thank you guys for your time! Best wishes, Poranny :)

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

103 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Why the control with gyroscope inverted when I make 180 degrees turn? 1 Answer

how do i stop/exit a coroutine? 1 Answer

Character Rotation point at Mouse 1 Answer

Very weird rotation axis bug 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