Watch out for Mirror's own FaceCamera script

I’m not sure if the version of Mirror that was used in the video contained this, but newer versions of Mirror contain an identically-named script called FaceCamera.cs that does a similar thing to the script in the video, however it does not set the “up” direction so it doesn’t behave the same way when the camera rotates. So just keep an eye out for that when adding the script to your HealthBar prefab, as you may be given two options.

Also, the maths code here is a little more complicated than it needs to be. Instead of applying rotations to unit vectors as done in the video:

    // original code
    transform.LookAt(
        transform.position + _mainCameraTransform.rotation * Vector3.forward,
        _mainCameraTransform.rotation * Vector3.up);

Instead the camera transform’s own axial unit vectors can be directly used:

    transform.LookAt(transform.position + _mainCameraTransform.forward,
                     _mainCameraTransform.up);

But perhaps even simpler, without having to worry about the transform’s world coordinates at all, just make the Health Bar transform “look” in the same direction and rotation as the camera itself, using a function designed for this exact situation:

    transform.rotation = Quaternion.LookRotation(_mainCameraTransform.forward,
                                                 _mainCameraTransform.up);

This sets the HealthBar’s forward and up vectors to match the camera’s in a combined transformation.

3 Likes

Thanks for the heads up on the FaceCamera conflict. These packages are changing constantly, so it’s good to have posts with updates.

I like the code on the face camera math, looks good. There are lots of ways to achieve the same orientation, some more or less complicated. There are also lots of slightly different ways to orient the camera that each can have their own behaviour and weird edge cases.

It’s good to play around with different ways to do this, to better understand the math and Unity’s different built in methods.

Privacy & Terms