Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);
}

}`

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading