Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 Mayakovskyi · Sep 11, 2012 at 10:41 AM · rotationquaternioneuleranglestilt

EulerAngles conversion Quaternion problem

Hello,

I want to tilt the body of a vehicle in order to simulate a (cheap but still) suspension.

Here is my code:

 float tilt = (Input.GetAxis("Horizontal") * 3.0f); // Rotate the body by -3/3 degree
     
 Vector3 targetRot = new Vector3(270 + tilt, body.transform.eulerAngles.y, body.transform.eulerAngles.z);
 Quaternion targetRotation = Quaternion.Euler(targetRot);
 body.transform.rotation = Quaternion.Slerp(body.transform.rotation, targetRotation, Time.time * 0.1f);


I'm tilting on the X axis because it corresponds to the body's forward.

My problem is the vehicle doesn't rotate correctly. It should go from 267 to 273 degrees depending on the Input. But it never goes below 270.

I found that the issue comes after "Quaternion.Euler". It always return an EulerAngle between 270 and 273.

Did I forget something ? How could I fix this ?

Thank you in advance !

Comment
Add comment · Show 2
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 Kryptos · Sep 11, 2012 at 11:21 AM 1
Share

There is more than one set of euler angles that can represent the same rotation.

Anyway your use of Quaternion.Slerp is wrong: Time.time*0.1f will always return a value > 1 after 10s.

Finally, note than an angle of 270 (= -90) is a bit strange. Why do you need to rotate your object that much?

avatar image Mayakovskyi · Sep 11, 2012 at 12:05 PM 0
Share

The imported model was a vehicle laying on the ground. I had to rotate it -90° in order to get it on its wheels.

Quaternion.Slerp does not clamp from 0 to 1 the speed value. $$anonymous$$aybe you are talking about $$anonymous$$athf.Lerp ?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Sep 11, 2012 at 12:44 PM

As @Kryptos said, there are several Euler angles combinations that result in the same rotation. This may become a big problem when you try to modify one of the Euler angles: Unity may return a completely different combination when one of the other angles crosses some "magical" boundary (changes signal, crosses 180 degrees etc.), and the resulting rotation suddenly becomes completely wrong.
The easiest way to tilt an object independently of its current orientation is to make it a child of the main object and modify its localEulerAngles:

  • Car hierarchy:

     Car <- main object, which moves and turns
         Body <- empty game object that will be tilted forth and back
           BodyModel <- actual body model
    
    
    
  • Body script:

     public float speed = 5.0f;
     
       void Update(){
         Vector3 tilt = new Vector3(Input.GetAxis("Horizontal") * 3.0f, 0, 0);
         transform.localEulerAngles = Vector3.Lerp(transform.localEulerAngles, tilt, speed * Time.deltaTime);
       }
    
    

The speed variable controls the tilting time: 5 means that the object will take about 1 second to reach 95% of the desired rotation, 10 means half second, and so on (0.1f would take 50 seconds! that's why the body didn't even move!). Input.GetAxis has its own smoothing time, which will add to the Lerp effect, thus you may have to use higher speed values.
NOTE: Notice that an extra parenting level was added: the Body empty object is sandwiched between the Car and the BodyModel. From your code, it seems that the model have flipped axes (a common problem when importing models), thus it must be rotated to (270,0,0) to be properly orientated. The Body empty object works as an interface between the model and the car: it can have localEulerAngles = (0,0,0) while allowing complete freedom to orient the model. You can eliminate this intermediate object, if you want: child the model directly to the Car, attach the script to the model and change the base X angle to 270, as you did in your code.

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 Mayakovskyi · Sep 11, 2012 at 01:11 PM 0
Share

Thank you very munch for the hint and explanations.

I will try this, but for the moment I found that only flipping the body by 2 ins$$anonymous$$d of 3 degrees won't made the rotation to act anormally (due to the multiple combination you mentionned).

Again, thank you, both of you :)

avatar image aldonaletto · Sep 11, 2012 at 11:39 PM 0
Share

Double check all possible car movements, since turning left or right when the vehicle is climbing a ramp may trigger the problem.

avatar image Keyserjaya99 · Aug 29, 2018 at 04:08 AM 1
Share

O$$anonymous$$G IT WOR$$anonymous$$S!!

Thank you so much!!

avatar image
0

Answer by Kryptos · Sep 11, 2012 at 11:15 AM

Since you are only changing the x-value (roll) of the euler angles why do you use quaternions?

 float tilt = (Input.GetAxis("Horizontal") * 3.0f); // Rotate the body by -3/3 degree

 Vector3 eulerAngles = body.transform.eulerAngles;
 eulerAngles.x = Mathf.LerpAngle(eulerAngles.x, 270+tilt, Time.deltaTime*0.1f);

 body.transform.eulerAngles = eulerAngles;

Depending on the situation, you may need to use localEulerAngles instead of eulerAngles.

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 Mayakovskyi · Sep 11, 2012 at 11:39 AM 0
Share

I already tryied this way, but there are two problems.

1) The body is not moving at all (it might be the last parameter of the lerpangle function. I don't know how to tweak it. When it is too low, the body does not move and when it's too high the movement is not smooth..)

2) When I turn left (and it happens only on left) my car is vibrating again and again. Like a continue shaking effect

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

11 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

Related Questions

ConfigurableJoint - angular positions (rotation) problem 1 Answer

Mouse Orbit on a Different XY Plane? 2 Answers

Matching 2 axes rotation only of 2 gameobjects 1 Answer

Rotation Jumping values (0 to 180) 1 Answer

Is there no way to get reliable angles from a rigidbody? 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