Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Noxury · Aug 13, 2019 at 04:10 PM · transformeuleranglesrecorddelta

Euler Angles without values being restricted to 0 - 360

How do I get non clamped 0 - 360 degree euler angles? Yes I am aware that Euler angles are defined this way but I want the correct delta for a stunt system.

.

The problem is, that I am adding the delta angles to a vector and if an eulerangle gets > 359.99.. or below 0 it wraps to the opposite value and it therefore adds or substracts 360. (This vector is information only, no transform rotation is applied to this)

.

The thing is: I want to record the total amount of spinning so if the object makes 2 clockwise rolls (X-Axis) the totalRot should be (720, 0,0), but if it makes a parabolic jump it should be (0,0,0) because the same delta angles are being substracted after the amplitude of the curve (see Figure 1)

.

Keep in mind that I want this on all Axes.

alt text

 IEnumerator CalculateTotalRot(){
     Vector3 lastEuler = transform.eulerAngles;
     Vector3 totalAngles = Vector3.zero
     while (!grounded) {
         totalAngles += (transform.eulerAngles - lastEuler); //total += Delta - Works unless object exceeds 0 or 360
         lastEuler = transform.eulerAngles;
         yield return null;
     }
     Debug.Log("Total Air Rotation: " + totalAngles );
 }

Debug to visualise the problem: alt text

howitshouldbe.png (34.5 kB)
results.png (37.6 kB)
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
2

Answer by Bunny83 · Aug 13, 2019 at 04:55 PM

Unity internally uses a Quaternion to store the rotation. Quaternions can not represent rotations larger than 180° since that's the same as rotating the other way round with a smaller amount. If you want to record the rotation you get from the physics system, you have to track it manually by accumulating the delta angles over time. Just ensure that the delta angle is always between -180 and 180°. So rotations faster than 180° per frame can't be detected reliably. Though that would be way too fast for most physics similations anyways.


From your question it's not clear if it's a 2d or a 3d game. If possible I would not rely on Unity's eulerAngles since they are calculated backwards from the quaternion and around gimbal lock orientations (euler angles issue) the euler angles will suddenly switch around. It's usually better to calculate the signed angle manually based on a reference system. If the rotation you're interested in is only about a single axis you can directly store the reference direction from the last frame and use Vector3.SignedAngle with the current one. If you rotate around the local x axis, use the objects forward direction and the right axis as normal.

 Vector3 lastFwd = Vector3.forward;
 float totalRotation = 0;
 void FixedUpdate()
 {
     float deltaAngle = Vector3.SignedAngle(lastFwd, transform.forward, transform.right);
     lastFwd = transform.forward;
     totalRotation += deltaAngle;
 }

Note due to number precision issues the totalRotation might drift over time. There are several possible solutions. One would be to always reset the totalRotation to 0 while on ground

Comment
Add comment · Show 9 · 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 sacredgeometry · Aug 13, 2019 at 04:59 PM 0
Share

You can pull the absolute rotation vector from the TransformUtils. Do you know anything about any performance repercussions for doing so. I cant imagine there would be any but always good to get a second opinion.

avatar image Bunny83 sacredgeometry · Aug 13, 2019 at 05:09 PM 0
Share

I don't quite understand what you mean. The orientation of a Transform is stored as Quaternion. The TransformUtils is a pure editor tool since the editor handles store the euler angles additionally which can actually exceed 360°. However those are human set values, not calculated angles at runtime. The question clearly was about a game feature, not an editor tool. Therefore the TransformUtil is completely out of the question.

avatar image sacredgeometry Bunny83 · Aug 13, 2019 at 05:12 PM 0
Share

I was looking for a work around I thought that the values were persisted like that but evidently they arent I will remove my answer.

Show more comments
avatar image Bunny83 · Aug 13, 2019 at 05:11 PM 0
Share

Huh? a downvote? Can the voter please leave a comment what you think is misleading or wrong? I'm happy to improve my answer.

avatar image sacredgeometry Bunny83 · Aug 13, 2019 at 05:19 PM 0
Share

I cant see one.

avatar image Noxury sacredgeometry · Aug 13, 2019 at 05:20 PM 0
Share

Lol @sacredgeometry your answer is gone I posted a comment to thank you for making an explanation video haha

Show more comments
avatar image josephan2 · Mar 07 at 10:55 PM 0
Share

This is brilliant thank you!

avatar image
1

Answer by sacredgeometry · Aug 13, 2019 at 06:01 PM

Not graceful but try this.

     float _theta = 0.0f;
     float _speed = 100;
     void Update()
     {   
         var x = _theta * _speed;
         var rotationCount = Math.Floor(Math.Abs(x) / 360);
         
         var rotation = Quaternion.Euler(0, x, 0);
         transform.SetPositionAndRotation(Vector3.zero, rotation);
 
         Debug.Log(rotationCount);
         _theta++;
     }
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 Pangamini · Aug 13, 2019 at 04:51 PM

You can keep and manipulate your angles in a Vector3. It's just 3 floats, nothing is clamping them. Then, you convert them to the rotation (quaternion) when you want to actually apply them to a transform.

However this won't work if your object rotation is changed by another source (eg. rigidbody). Reading eulers from the transform will always be clamped

Comment
Add comment · Show 4 · 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 Noxury · Aug 13, 2019 at 05:24 PM 0
Share

Hey, According to Bunny83s answer unfortunalty, they are

avatar image Pangamini Noxury · Aug 13, 2019 at 05:28 PM 0
Share

Think again. Vector, it's just a bunch of floats that you can work with. You can write any value to it, and then you convert it one-way to the rotation. Not the other way (rotation of the transform to eulers and back to your vector). There's nothing overwriting your vector with clamped values, so how can it happen?

avatar image sacredgeometry Pangamini · Aug 13, 2019 at 05:38 PM 0
Share

@Noxury

If you are the one controlling the values then its fine. That was my answer too. I assumed that was not applicable some how and that the rotation was as a side effect of some other behaviour.

If the later is the case then no. It doesnt work. If the former then yeah, my original post and Panga$$anonymous$$is are fine.

Show more comments

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

143 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use a single rotation axis of a freely rotating object 1 Answer

Rotation Jumping values (0 to 180) 1 Answer

Rotation promlems past 90 and 270 3 Answers

c# modify only one axis of a quaternion 2 Answers

transform.eulerAngles rotating speed (quick question) 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