Comments and answers for "Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle."
http://answers.unity.com/questions/1086228/calculating-the-difference-between-forward-directi.html
The latest comments and answers for the question "Calculating the difference between forward direction and velocity direction to set the body roll of my vehicle."Comment by LewisGerschwitz on LewisGerschwitz's comment
http://answers.unity.com/comments/1086344/view.html
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;Thu, 22 Oct 2015 18:10:20 GMTLewisGerschwitzComment by Glurth on Glurth's answer
http://answers.unity.com/comments/1086325/view.html
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.Thu, 22 Oct 2015 17:14:15 GMTGlurthComment by LewisGerschwitz on LewisGerschwitz's answer
http://answers.unity.com/comments/1086304/view.html
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.Thu, 22 Oct 2015 16:48:52 GMTLewisGerschwitzAnswer by Glurth
http://answers.unity.com/answers/1086270/view.html
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)Thu, 22 Oct 2015 15:46:31 GMTGlurth