Alternate ways of getting the camera to face

I recall in a much earlier lecture, Rick got the camera to follow the character by childing the Camera to a component. Can we do something similar here to make the text face the screen?

Could we also use the LookAt method? Or am I barking up the wrong tree?

There are multiple ways to do this, and to be fair, it’s a very simple thing to achieve, so I don’t think’s there’s a wrong answer. As far as I’m aware, the LookAt function uses quaternions, which are far more complicated than vectors, but I don’t think there would be much of a difference performance-wise if you use Sam’s approach or yours.

As for childing the text to the camera, I suppose you could child the text and then set it’s rotation in all axis to 0 on enable so it always faces the camera, but that would cause an issue, the text would move with the camera, you would need to prevent the text from moving which would be slightly more complicated.

But then again, if you don’t end with 30+ lines of code, use the approach you prefer, there won’t be much of a difference.

Actually, childing the text to the camera won’t make it face the camera. It will make it follow the camera, but face whichever way it is set to face. You could use LookAt but bare in mind that this will point the text’s forward vector towards the camera and text’s forward vector usually points away from the camera. You could get the direction from the camera to the text and use that direction. Something like

transform.LookAt((transform.position - Camera.main.position).normalized);

Edit
This may not work properly. I think the above would point to a spot around the origin because the normalised vector is not translated to the text. Lemme consult my plethora of code


Yes, you need to also offset the position

Vector3 lookAtTarget = (transform.position - Camera.main.position).normalized;
transform.LookAt(transform.position + lookAtTarget);

Childing the text to the camera will definitely not work. We want it to say with the character, not move with the camera.
Transform.LookAt will point the display to the camera, but you’ll find it looks a bit wonky, Health bars directly in front of the camera (center screen) will look ok, but ones on the side will take on a bit of that fish eye lense effect.
By setting transform.forward = Camera.main.transform forward, you ensure that the transform is facing the camera and that it is properly aligned with the camera’s field of view.

1 Like

Guys, @Brian_Trotter @bixarrio, What about a fun bet? I can make it work by childing the text to the camera, I bet you a… … virtual burrito? … … I mean, it won’t be particularly efficient, but it can be done.

Can you get it to work in less than 30+ lines of code? :stuck_out_tongue:

I certainly can! :rofl:

I’ll do it for fun and come back with the code and a gif, but it will take me roughly 15 hours, I need to sleep first :sweat_smile:

1 Like

I just remember about this, sorry, was busy with work.

I know you guys know this can be done, I’m just doing it for the lulz.

lolSolution

Transform stick;

private void Awake()
{
    stick = transform.parent;
}

private void OnEnable()
{
     transform.parent = Camera.main.transform;
     transform.rotation = Camera.main.transform.rotation;
}

private void Update()
{
    transform.position = stick.position;
}

Not quite what I had in mind, but
image

2 Likes

Privacy & Terms