• 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
Question by LewisGerschwitz · Oct 22, 2015 at 03:28 PM · rotationvelocitydirectionforwardroll

Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle.

Hi all,

As a relatively new Unity developer I'm having trouble coming up with a solution, possibly due to my vague understanding of the relationship between quaternions and euler angles.

Adding body roll to the model of my vehicle by calculating the difference in y angle between the angle the vehicle is facing and the direction of velocity/momentum. By adding it to the model instead of the parent/controlling object I was hoping to avoid over-complicating the steering process.

Currently my pseudo-code for this: (Code at its most basic level, disregarding euler to quaternion.)

 rollAngle = ( parent.forward.y - parent.rigidbody.velocity.y ) / 2; 
 
 transform.eulerAngle = new Vector3 (0, 0, rollAngle);

Should mean that when the direction of the vehicle is perpendicular to its momentum on the y-axis it will produce a 45 degree body roll.

alt text

So to sum up, I'm looking to take the y rotation of my object and subtract the y rotation of its velocity divide the value by two and then use it to add body roll to my object on the z axis. I think I need to convert both to eulerAngles and subtract them that way, but I haven't yet discovered a way to do so.

Thanks for taking the time to help me out!

bodyrollhelp.jpg (155.2 kB)
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Glurth · Oct 22, 2015 at 03:46 PM

Your code looks like its does ALMOST exactly this: "To to sum up, I'm looking to take the y rotation of my object and subtract the y rotation of its velocity divide the value by two and then use it to add body roll to my object on the z axis. "

With the one exception: you are REPLACING the rotation of the object with this, rather than adding to it.

  transform.eulerAngle = new Vector3 (0, 0, rollAngle);

If you wish to rotate the existing orientation, by that angle try something likethis:

  transform.rotation = transform.rotation * Quaternion.Euler(0, 0, rollAngle);  

(it uses this operator to combine the rotatations: http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html)

Edit: note-the above operator "adds" the second Quaternion to the First. If you wish to reverse the rotation of the second Quaternion to do a "subtraction", use http://docs.unity3d.com/ScriptReference/Quaternion.Inverse.html ,like so:

  transform.rotation = transform.rotation * Quaternion.Inverse(Quaternion.Euler(0, 0, rollAngle));  

(silly in this case, because you could have just used rollAngle * -1 to begin with)

Comment

People who like this

0 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 LewisGerschwitz · Oct 22, 2015 at 04:48 PM 0
Share

Thank you for the tip, but I feel like you haven't fully understood what I'm trying to do.

My issue isn't with adding the rollAngle to my object, it's with calculating the rollAngle.

avatar image Glurth · Oct 22, 2015 at 05:14 PM 1
Share

I am indeed NOT quite clear on what the problem is.

Is this the part you are having trouble with: " calculating the difference in y angle between the angle the vehicle is facing and the direction of velocity/momentum."?

If so, you should be able to use http://docs.unity3d.com/ScriptReference/Vector3.Angle.html, which takes two vectors, and returns the angle between them. Sounds like you might want to trim the input vectors and set their Z coordinate to 0, assuming you only want the angle on the XY plane.

avatar image LewisGerschwitz Glurth · Oct 22, 2015 at 06:10 PM 0
Share

Thanks, that might be exactly what I'm looking for.

Edit: Got it working;

 private Vector3 eulerAngles;
 private Vector3 velocityVec;
 private Vector3 forwardVec;
 public float angle;
 private int cross;
 
 velocityVec= transform.parent.GetComponent<Rigidbody>().velocity.normalized;
 velocityVec.y = 0;
 
 forwardVec= transform.parent.forward.normalized;
 forwardVec.y = 0;
 
 angle = Vector3.Angle(velocityVec, forwardVec);
 cross = Vector3.Cross(velocityVec, forwardVec).y < 0 ? 1 : -1;
 
 angle = Mathf.Clamp(angle, -45, 45); //Clamp to stop it over-rolling on sharp turns
 
 eulerAngles = new Vector3(transform.parent.rotation.eulerAngles.x, transform.parent.rotation.eulerAngles.y, cross*angle);
 
 transform.eulerAngles = eulerAngles;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

how to find rotation from direction of velocity 1 Answer

Rigid Body relative speed 1 Answer

Rigidbody slowing right down when colliding only when I'm rotating the velocity to always be going forward 1 Answer

Rotation being forced away from what I want 1 Answer

(s)lerp y-rotation to the direction of velocity 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