Implement Unity Transform.LookAt function.


I’m trying to implement the Transform.LookAt function that can be found here: Unity - Scripting API: Transform.LookAt

This function calculates Yaw, Pitch and Roll and updates the rotation of the object.

Example(Update position and rotation of the sun):

sun.localPosition = Vector3(?, ?, ?);
sun.LookAt(targetPos, upVector) //this should be the same as glm::lookAt(sunPos, targetPos, upVector) I think?

I found another guy who tried to implement the whole Transform class: opengl - Build unity like Transform class - Game Development Stack Exchange

And I found his implementation of LookAt:

void Transform::LookAt(glm::vec3 targetPosition)
        if((targetPosition - m_Position) == glm::vec3(0,0,0)) return;
        glm::vec3 direction = glm::normalize(targetPosition - m_Position);
        m_Rotation.x = asinf(-direction.y)* RadToDeg;
        m_Rotation.y = -atan2f(-direction.x, -direction.z)*RadToDeg;

        m_NeedUpdate = true;

But “m_Rotation” does only contain Pitch and Yaw, not Roll. How did they(Unity) calculate Roll?

Thanks in advance.

Why don’t you just simply copy the algorithm used in gluLookAt function?