• 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 DGKN · Jul 12, 2014 at 02:01 PM · flips

Rigidbody.MoveRotation - How to count backflips/frontflips ?

Hi guys, I want my game to display the number of flips the character does, how can I implement that ?

     void FixedUpdate () 
     {
         float rotate = Input.GetAxis ("Vertical");
         rigidbody2D.MoveRotation(rigidbody2D.rotation + 20 * Time.deltaTime * rotate * 100);
     }



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 robertbu · Jul 12, 2014 at 02:30 PM 0
Share

http://answers.unity3d.com/questions/651849/count-the-amount-of-flips-and-object-makes.html

3 Replies

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

Answer by DGKN · Jul 12, 2014 at 05:06 PM

Seems like I've found a solution :

     float flips = 0;
     float deltaRotation = 0;
     float currentRotation = 0;
     float WindupRotation = 0;
 
 deltaRotation = (currentRotation - transform.eulerAngles.z);
         currentRotation = transform.eulerAngles.z;
         if (deltaRotation >= 300) 
             deltaRotation -= 360;
         if (deltaRotation <= -300) 
             deltaRotation += 360;
         WindupRotation += (deltaRotation);
 
         flips = WindupRotation / 360;

This shows me how many degrees my character rotates even if it's more than 360°. Then I just have to divide it by 360 to know how many flips it has done.

Example : Player rotates -720° -> -720/360 = -2 (Which means 2 backflips) Player rotates +720° -> 720/360 = 2 (Which means 2 frontflips)

It seems to work right now, but do you guys think this is the proper way to count flips ?

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 dsada · Jul 12, 2014 at 02:09 PM

You can check the transform.eulerangles value in the LateUpdate() function. Lets say your character did a flip if it reaches the 350-360 interval. Then you increment your flip value and set a boolean variable to dont increment until it does not leave this interval. After its left you can check again.

This is the first idea that came into my mind maybe there are more elegant ways to check but its not trivial because of the repeation of the 0-360 values.

Comment
Add comment · Show 3 · 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 DGKN · Jul 12, 2014 at 02:31 PM 0
Share

Thanks, but how do I check if it has reached the 350-360 interval though ? Is this the proper way to check it ?

 if (transform.localEulerAngles.z > 350 && transform.localEulerAngles.z < 360)
             Debug.Log ("flip");
avatar image dsada · Jul 12, 2014 at 02:48 PM 0
Share

yes, thats good. However i've got really weird experiences with eulerangles. Sometimes its moving between -90 - 270 sometimes its 0 - 360. Observe your character and adjust the values to your needs.

avatar image DGKN · Jul 12, 2014 at 03:25 PM 0
Share

This is what I've found, but I can't code it, if anyone knows how to code this I'll be really happy.

"Basically, save the upright rotation into one variable at the start, for example "Quaternion normalRotation". Then, create a new variable that is a Quaternion that is 359 degrees different from normalRotation on the same axis. Whenever your player reaches this newRotation, you can count it as a back/front flip. This is pseudo-pseudo code, but I hope this helps. "

avatar image
0

Answer by robertbu · Jul 12, 2014 at 03:40 PM

In many situations, eulerAngles does not give back what you want. There are multiple eulerAngle representations for any rotation, and sometimes Unity changes the representation. For 2D, you can use ATan2(). Assuming you are rotating counterclockwise, you can do something like this:

 Vector3 dir = transform.right;
 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 if ((prevAngle > -10.0f && prevAngle < 0.0f) && angle >= 0.0) {
      flips++
 }
 prevAngle = angle;

Note that Mathf.Atan2() returns angles in -180 to 180. The sign will flip when it goes form -180 to 180, so you cannot just check the sign. That is way the 'prevAngle > -10.0f' is included. 'prevAngle' is a class instance variable. Atan2 returns 0.0 for Vector3.right. So when the 'transform.right' is equal to 'Vector3.right' the angle will be 0.0.

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 DGKN · Jul 12, 2014 at 04:13 PM 0
Share

Thanks robert, I'm playing with your code to make it work because right now, when I rotate a few degrees left (eg: -20°), it increments flips and then it seems to work but when I rotate at high speed, it doesn't seem to save flip counts anymore

Should I use it in Update or FixedUpdate ? Right now, I'm using Update

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

how can I make a code with a "flip" 0 Answers

Trying to make a sprite flip based on walking direction as well as camera position 0 Answers

am I using degrees, radians, rotation or quaternion ? 2 Answers

Tracking Object Rotation? 2 Answers

Moving rigidbodies with MovePosition/MoveRotation outside of FixedUpdate 3 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