Search code examples
unity-game-engine3dspritehelpernlp-question-answering

How do get a 2D sprite to face the camera in a 3D unity game?


I've been trying to get it to work but most of the codes are either outdated or doesn't work can anyone help i need a code for unity 2021 ?

I tried finding some codes but they are pretty old like from 2016


Solution

  • The term "billboard" in computer graphics refers to an object that always faces the camera. You can try a billboard component, like so:

    public class Billboard : MonoBehaviour {
        public Transform cam;
        private void Start() {
            cam = Camera.main.transform;
        }
        void LateUpdate() {
            transform.LookAt(transform.position + cam.forward);
        }
    }
    

    A follow camera behaviour should always be implemented in the LateUpdate because it tracks objects that might have moved inside the Update. Also if your sprite is inside a canvas make sure its in world's space, so that it is a 3D world object and it can rotatute. Canvas space is an option in the canvas component itself.