This site uses strictly necessary cookies. More Information

X- Home /

**Question**by SoxwareInteractive · Sep 15, 2016 at 12:45 PM · transformquaternion.lookrotationangleaxis

# LookRotation with correct roll angle (using angle axis)

Hi everyone,

I have a UnityEngine.Transform which is rotated/oriented in any arbitrary way in 3D space. Also there is a 3D target point in world space. By using Quaternion.LookRotation() it's possible to have an object pointing from the transforms position to the target point. But I also want the pointing object to be be rolled in such a way, that it represents the transforms rotation angle ("roll / twist") along the axis between the transforms position and the target point:

This code doesn't show the correct roll angle as it doesn't take the transforms rotation into account yet:

```
Vector3 dir = worldSpaceTargetPoint - transform1.position;
pointingObjectTransform.position = transform1.position;
pointingObjectTransform.rotation = Quaternion.LookRotation(dir, Vector3.up);
```

So how can I change the code to get the correct roll angle? I guess I would need a method like Quaternion.AngleAxis(float angle, Vector3 axis) that takes a quaternion and an axis as input parameters and returns the angle. Sadly I found no method for that... Thanks in advance!

https://docs.unity3d.com/ScriptReference/Transform.LookAt.html and https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

These function takes two parameters, the first is the direction you want to look, and the axis you mentioned in your question.

The second param defines a rotation around this axis, by specifying which direction is "up". Imagine you have a camera at the origin pointing along the axis, at the target. the "up direction" is the direction that the top of the camera's viewport points. **In your example code you are using Vector3.zero, for this parameter**, which is invalid input, since it does not define a direction. Usually people use `Vector3.up`

for this param, but, of course, it varies by situation: I'm not quite clear what value you want to use in there, from your description.

Another commonly used second parameter is: `transform.up`

for the second param (convert's the transform's local/modelspace "Up", to a world-space direction.)

Hi @Glurth,

thanks for your answer! I accidentally wrote Vector3.zero when I composed this simplified version of my real script. I corrected my original post. Sorry for that.

You are right, Transform.up would be exactly what I need and I already tried that but the problem is due to the fact that the transform can have any arbitrary orientation (rotation) in world space, it could be that transform.up is pointing in the same direction as the rotation axis. Which results in undesired rotations.

So maybe the real question is, how to get an up vector that is perpendicular to the viewing direction (target point - transform.position) and represents the correct "roll" angle (see picture above)? And to get this roll angle, I would need a method like Quaternion.AngleAxis(float angle, Vector3 axis) but which returns the angle and uses the axis and the quaternion as input parameters. Does someone know the maths behind this method?

"how to get an up vector that is perpendicular to the viewing direction (target point - transform.position) and represents the correct "roll" angle (see picture above)?"
I'm unclear why `transform.up`

is not sufficient for these needs. This is specifically different from `Vector3.up`

, in (pretty-much, unless I misunderstand), the way you describe.

Edit: oh, I misread your answer- I think you see this already. Obviously I'm just not understanding what your looking for. What do you expect "up" to be, in the case you mentioned, where the transform's original up, happened to be pointing directly at the target?

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

### Follow this Question

### Related Questions

Quaternion.LookRotation() doesn't look down 2 Answers

Make object lay flat on a sphere + look up? 0 Answers

Finding roll angle 0 Answers

make player move in direction it's facing 2 Answers

Object rotation 2 Answers