123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- #if ZED_HDRP || ZED_URP
- using UnityEngine.Rendering;
- #endif
- /// <summary>
- /// Makes the GameObject turn to face the target object each frame.
- /// Used for an outline effect on the ZED planetarium sample's sun, as it's drawn by a quad.
- /// </summary>
- public class LookAtCamera : MonoBehaviour
- {
- #if !ZED_HDRP && !ZED_URP //OnWillRenderObject doesn't work in SRP, so use a callback from RenderPipeline to trigger the facing instead.
- void OnWillRenderObject()
- {
- FaceCamera(Camera.current);
- }
- #else
- /// <summary>
- /// The ZEDManager that the object will face each frame. Faces the left camera.
- /// </summary>
- [Tooltip("The ZEDManager that the object will face each frame. Faces the left camera.")]
- public ZEDManager zedManager;
- private Camera zedLeftCam;
- private void Start()
- {
- if(!zedManager)
- {
- zedManager = FindObjectOfType<ZEDManager>();
- }
- if (zedManager) zedLeftCam = zedManager.GetLeftCamera();
- }
- private void LateUpdate()
- {
- if (zedLeftCam) FaceCamera(zedLeftCam);
- }
- #endif
- void FaceCamera(Camera cam)
- {
- Camera targetcam = cam; //Shorthand.
- //Make sure the target and this object don't have the same position. This can happen before the cameras are initialized.
- //Calling Quaternion.LookRotation in this case spams the console with errors.
- if (transform.position - targetcam.transform.position == Vector3.zero)
- {
- return;
- }
- transform.rotation = Quaternion.LookRotation(transform.position - targetcam.transform.position);
- }
- }
|