• 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 Graeme P · Mar 20, 2012 at 01:32 PM · angledetectionflips

Tracking Object Rotation?

Hi,

I'm working on a game where the player can do flips, I need to find a method of a script to 'watch' how many degrees the player has turned, so for example - If, full 360 to the right, then = frontflip. If, full 360 to the left, then = backflip.

But I'm really not too sure where i would start with this...

Any help would be really appreciated!

Thanks.

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
2

Answer by aldonaletto · Mar 20, 2012 at 03:13 PM

I would use a different approach: have a float variable curAngle, modify it according to the controls and then set the player rotation to curAngle using eulerAngles. Since you must not set individual eulerAngles components, save its initial value in a variable euler, then modify only the desired axis. In the example below (attached to the player), the player turns around the Y axis:

var curAngle: float = 0.0; // current angle about Y var turnSpeed: float = 15.0; // degrees per second private var euler: Vector3; // save the initial euler angles

function Start(){ euler = transform.eulerAngles; // initialize the euler variable }

function Update(){ // modify the curAngle variable using the controls curAngle += Input.GetAxis("Horizontal") turnSpeed Time.deltaTime; // set the Y euler angle in modulo 360 euler.y = curAngle % 360; // update the player rotation transform.eulerAngles = euler; } You can always know the current angle reading curAngle.

EDITED: Ok, if you want to know the current angle about Y, you can just read transform.eulerAngles.y
This will work fine until the eulerAngles x and z are not far from zero: weird y angles may be returned at some points otherwise. The property eulerAngles is just transform.rotation in a 3-axes format; since 3-axes format is redundant (0,180,0 is the same rotation as 180,0,180) many different xyz angle combinations may be returned for the same rotation. Unity tries to return the most logical combination, but when the x, y and z are all non-zero, some weird values may be returned.
Case you have problems with eulerAngles.y, try the simple function below, which returns the current rotation angle around Y - the angle is "extracted" from the quaternion components:

function AngleAboutY(q: Quaternion): float { var ang = 2*Mathf.Rad2Deg*Mathf.Acos(q.w*Mathf.Sign(q.y)); if (ang > 180) ang -= 360; return ang; }

// read the current object rotation with something like this: angle = AngleAboutY(transform.rotation);

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 Graeme P · Mar 20, 2012 at 03:32 PM 0
Share

If I understood this correctly, then this script would actually affect the rotation of the player them self - or am I misunderstanding it?

Sorry if i was unclear, my game is physics based, so the player will do flips and tuck to adjust their spin speed, like in real life. So that's why I need something that checks just for the angle away from the starting position, so i could then allocate some kind of point rewards for successful flips.

Thank-you for your help Aldo, I really appreciate it!

avatar image aldonaletto · Mar 21, 2012 at 03:37 PM 0
Share

Yes, the approach above isn't a good idea since you're using physics. I edited my answer to include two other ways to know the current direction.

avatar image EVOin3D · Jul 03, 2015 at 09:28 AM 0
Share

This is such a simple and elegant solution. Thanks aldo!

avatar image
1

Answer by m4c0 · Mar 20, 2012 at 03:22 PM

You can store the euler angles when the movement starts (ex: in your "jump" script), then compare with new angles when the player hits the ground. This hit-check can be an OnTriggerStart event, or simply waiting for its y-speed be equal to zero.

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 Graeme P · Mar 20, 2012 at 03:44 PM 0
Share

That certainly does sound like a logical way of doing things. I'l have to give it a go when I get home from work.

Thanks m4c0!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Delete object when two collision or more 1 Answer

How to check collision with specific object 1 Answer

Coconut collision detection script 0 Answers

Changing the shoot angle on one robot changes all of the other robots' variables? 1 Answer

Is there a way to detect which way an object is facing? 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