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
2
Question by Mystic_Quest · Jul 26, 2018 at 12:10 PM · rotation detection

Detect when rotation stops

Got a cannon that I move with transform following the mouse pointer. My problem is that I can't detect when the cannon stops rotating. It doesn't have any angular velocity etc probably since it moves with transform. Any way to make an "if cannon doesn't rotate" statement?

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
1

Answer by Keith-Rek · Jul 26, 2018 at 01:07 PM

Cannons! sounds cool :D How about something like this?

     public Quaternion rotationStored;
     public Quaternion currentRotation;
 
     public float maxErrorAllowedPerFrame;
 
     public bool inRange;
 
     // Use this for initialization
     void Start () {
         rotationStored = transform.rotation;
         currentRotation = transform.rotation;
     }
     
     // Update is called once per frame
     void Update () {
 
         currentRotation = transform.rotation;
 
         if (Mathf.Abs (Quaternion.Angle (rotationStored, transform.rotation)) < maxErrorAllowedPerFrame || currentRotation == rotationStored) {
             
             inRange = true;
 
         } else {
             
             inRange = false;
 
         }
 
         rotationStored = transform.rotation;
 
         if (inRange) {
 
             // do somethin'
 
         }
 
     }
 
     // Keith Rek, 2018-07-26.

Tested on my Editor, and it worked (almost) perfectly.

I found out that both the 'Quaternion.Angle' function AND the 'Vector3.Angle' function was NOT accurate, both of them returning an error of 0.02 or so, even when the two rotation values were exactly the SAME.

So, I came up with a new condition

      currentRotation == rotationStored

in order to cover up the function's inaccuracy.

      public Quaternion rotationStored;
      public Quaternion currentRotation;

'rotationStored' is the value that gets stored on every end of the frame, and the 'currentRotation' is the value that gets stored at the start of it. The 'Quaternion.Angle' function will print out the difference in angle between the two 'Vector's, and 'Mathf.Abs' function will turn them into an 'Absolute' value.

      public float maxErrorAllowedPerFrame;
  
      public bool inRange;

You can tinker with the 'maxErrorAllowedPerFrame' variable to set its responsiveness against the detection or so, and finally, the boolean value 'inRange' will turn true when there is no rotation detected. The rest is up to you.

Comment
Add comment · 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
0

Answer by AdityaViaxor · Jul 26, 2018 at 12:21 PM

Yes,Assuming its 3d (if 2D game check z value) game There is way get trasform.rotation.y value and put if condition for ex.

if(trasform.rotation.y>=90 or 120 or whatever you want)

{

// do someting

}

Comment
Add comment · 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

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

88 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

Related Questions

Turn Steering wheel on Key press 0 Answers

How can I make player rotation control camera's x rotation and mouse control it's y rotation? 1 Answer

Getting the rotation of raycast target object and applying to the player (noob question) 1 Answer

How would i track rotational direction from my rotation cordinates? 1 Answer

Float value = gameobject.rotation 2 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