Explanation Of Why This 'Always-Look-At-Camera' Script Works

Greetings

Found a very helpful snippet of code on the community wiki:

void Update()
{
    transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.back,
        m_Camera.transform.rotation * Vector3.up);
}

My question is why does this work and actually make the UI element always face the camera? Could you explain how the math on this works?

Its a really short solution and I love how concise it is, but I can’t wrap my head around how it works!

The math in the code is actually quite simple but the way it is written is overly complex. It can be simplified to

transform.LookAt(transform.position-m_Camera.transform.forward, 
                 m_Camera.transform.up);

From the position of the transform (transform.position) the target of the UI element is looking back (-m_Camera.transform.forward) at the camera. The new up of the UI element is the camera’s up (m_Camera.transform.up).