- Home /

# 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.

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!

**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)

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.

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.

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

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.