How do I get the current active camera?
I have a billboard texture and it obviously needs to face the camera, but because I have vehicles and such the camera changes and all of them are not tagged "MainCamera" for important reasons. I tried to do something like this but it didn't work. Script:
using UnityEngine;
using System.Collections;
public class Billboard : MonoBehaviour
{
public Camera m_Camera;
void Update()
{
m_Camera = Camera.current.GetComponent<Camera>();
}
//Orient the camera after all movement is completed this frame to avoid jittering
void LateUpdate()
{
transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward,
m_Camera.transform.rotation * Vector3.up);
}
}
Comment
Hi man! I am also looking for this answer... Amazing, since Nov 2018 nobody answered this question.
Answer by JPhilipp · Feb 16, 2020 at 12:35 PM
Use
Camera camera = (Camera) FindObjectOfType(typeof(Camera));
if (camera)
{
Debug.Log("Found!");
}
Answer by unity_0kHHQVY9-nX5hg · May 22, 2021 at 09:33 PM
This is how i did it transform.LookAt(Camera.allCameras[0].transform.position);
allCameras is an array of all active cameras in your scene, so you should obviously have only one camera active at all times, and access the first element in that array. Hope this helped anyone!