How do I stop having my Z axis do a 180 on me

I’m trying to create a billboard effect for my canvas in vr where it would follow my player on the y axis (side to side), for the most part my code works and it only spins around itself when i go around it. However when I do a full loop for some reason my z axis goes up 180 degrees displaying my canvas upside down. How would I fix this problem?

This is my current code
`public class Billboard : MonoBehaviour
{
/Ce script sert a creer un effet billboard sur nos texte, alors il suiveront la camera du joueur/
public Camera _playerCamera;

private void FixedUpdate()
{
    transform.LookAt(transform.position + _playerCamera.transform.rotation * Vector3.forward, _playerCamera.transform.rotation.y * Vector3.up);
}

}`

Results:
enter image description here

When I do a full loop:
enter image description here

>Solution :

It looks like you’re overcomplicating it. Try something simpler, like:

transform.LookAt(_playerCamera.transform, _playerCamera.transform.up);

That function takes a transform as a target, so just make it look directly at the camera.

If you want it to only rotate on the Y axis, then you can do something like this:

Vector3 flatOffset = _playerCamera.transform.position-transform.position;
flatOffset.y = 0;
transform.rotation = Quaternion.LookRotation(flatOffset, _playerCamera.transform.up);

Also, this would probably be better in Update() since it affects something that needs to be done per rendered frame, rather than in FixedUpdate(), which is more for background simulation stuff like physics and may run more than once per frame.

Leave a Reply