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
1
Question by masterhoo · Jun 16, 2017 at 01:29 AM · unity 5vector3anglefloat

get Pitch and Roll values from object

I'm making a rollercoaster game, where I need to get the roll & Pitch values from an object (cube), how do I get those values? I tried... but nothing, any clue?

  var tempRight = transform.right;
  tempRight.y = 0;
 float pitch = Vector3.Angle(transform.right, tempRight)*Mathf.Sign(transform.right.y);
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Jun 16, 2017 at 07:06 AM

First of all keep in mind that pitch, yaw and roll (euler angles) is not a unique way to represent a rotation in 3d space. There are always at least two ways you can set your angles to achieve the same rotation. That's why it's impossible to get a clear answer for both angles. For example if your up vector points straigned down, do you currently perform a roll (around the local z axis) or a looping (around the local x axis)?

Anyways, to get at least a continuous angle for both, you want to have at least one world reference. In both cases i recommend the up vector.

For pitch (around local x-axis):

 var right = transform.right;
 right.y = 0;
 right *= Mathf.Sign(transform.up.y);
 var fwd = Vector3.Cross(right, Vector3.up).normalized;
 float pitch = Vector3.Angle(fwd, transform.forward) * Mathf.Sign(transform.forward.y);

For roll (around local forward axis)

 var fwd = transform.forward;
 fwd.y = 0;
 fwd *= Mathf.Sign(transform.up.y);
 var right = Vector3.Cross(Vector3.up, fwd).normalized;
 float roll = Vector3.Angle(right, transform.right) * Mathf.Sign(transform.right.y);

Note that in those cases pitch and roll are independet from each other. However if you perform a looping and you pass the 90° mark (either up or down) the roll will suddenly switch from 0 to 180 or -180. Likewise when you perform a roll, when you pass +-90° the pitch will suddenly switch from 0 to 180 or -180

So those two methods are just a way to determine the current pitch / roll values but not to determine which kind of movement you carry out. For this you would need to analyse the movement over time.

Though there are edge cases where you can't tell if you should call it a roll or a looping. If you have a "screw" looping at a 45° angle you equally perform a looping as well as a roll. It's in general hard to define pitch and roll in absolute values. If you bank your cart 90° (so it lies on the side) and perform a "flat" looping, does that count as pitch? Locally it would, globally it doesn't

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
1

Answer by mjc33 · May 30, 2020 at 12:23 AM

You can also get the same result like this:

     pitch = (transform.rotation * Quaternion.Euler(180, 0, 0)).eulerAngles.x;
     roll = (transform.rotation * Quaternion.Euler(0, 0, 180)).eulerAngles.z;

Also it's possible to do this

     pitch = (transform.rotation * Quaternion.Euler(180, 0, roll)).eulerAngles.x;
     roll = (transform.rotation * Quaternion.Euler(pitch, 0, 180)).eulerAngles.z;

Which gives a result that's more like an absolute pitch and roll value (not affected by the rotation of the other)

However for most use cases the most robust way to get pitch and roll is using a Quaternion to Euler formula that works with the x,y,z,w components of the Quaternion directly, see this answer for more info

https://answers.unity.com/questions/416169/finding-pitchrollyaw-from-quaternions.html

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 masterhoo · Jul 01, 2017 at 02:37 PM

Hi, @Bunny83 I'm looking at this, and trying to think if it's worth the extra complication:

  var right = transform.right;
  right.y = 0;
  right *= Mathf.Sign(transform.up.y);
  var fwd = Vector3.Cross(right, Vector3.up).normalized;

If you take the cross of the right vector and the up vector, I think you'll get the forward vector. So I might go:

   var fwd = transform.right;
   fwd.y = 0;

which would cut out the middle.

Assuming I'm correct that they get the same result. Maybe there's a useful difference that I'm not thinking of? hope you help me with this issue thank you

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 unity_rVEF6Iu0fmliUg · Apr 28, 2020 at 01:44 PM 0
Share

Not sure, but I think that can be a direction is different between the two

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

136 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

Related Questions

How to make the calculated angle respective of the players rotation? 1 Answer

Cannot modify a value type return value of `UnityEngine.Transform.position'. Can anyone help ? 1 Answer

Problems with Vector3.angle and angle to target 1 Answer

making a camera that moves depending on mousepos.y 0 Answers

Uses of Vector3.Dot 0 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