LookAt only when camera position changes

Hi!

I’m trying to optimize the script use to look at the main camera due to I have plenty of objects. I’m trying just to Update the LookAt when the camera position has changed, but I don’t know how to do it. My script is the following one:

    private SpriteRenderer sRenderer;
    private Vector3 position;
    private Camera mainCamera;

    private void Awake()
    {
        mainCamera = Camera.main;
        position = this.transform.position;
        sRenderer = this.GetComponent<SpriteRenderer>();
    }
    
    protected virtual void LateUpdate()
    {
        if (this.sRenderer.isVisible)
        {
            if (Time.frameCount % 30 == 0)
            {
                transform.LookAt(position + mainCamera.transform.rotation * Vector3.forward, mainCamera.transform.rotation * Vector3.up);
            }
        }
    }

I can suggest you events. Create an event to detect your camera position change and register your object’s script to that event.

//Attach this to main camera and detect position changes here
 public class CameraPositionChange : MonoBehaviour {
 
     public delegate void camPositionDelegate(Vector3 camRotation);
     public static event camPositionDelegate camPositionChangeEvent;

     private Vector3 lastPosition;

     public static void CamPositionChange(Vector3 camRotation) {
         if (camPositionChangeEvent!= null) {
             camPositionChangeEvent(camRotation);
         }
     }

    void Start(){
        lastPosition = transform.position;
    }

    void Update(){
          if(transform.position != lastPosition){
                 lastPosition = transform.position;
                 CamPositionChange(transform.rotation.eulerAngles);
          }
    }
}

//this is your already existing script that you attach to target object. 
public class YourAlreadyExistingClass : MonoBehaviour {
         private SpriteRenderer sRenderer;
         private Vector3 position;
         //private Camera mainCamera; //you won't need this anymore
         private void Awake()
         {
             //mainCamera = Camera.main;
             position = this.transform.position;
             sRenderer = this.GetComponent<SpriteRenderer>();
         }
         
        private void OnEnable() {
             CameraPositionChange.camPositionChangeEvent += OnCamPositionChange;
         }
     
         private void OnDisable() {
             CameraPositionChange.camPositionChangeEvent -= OnCamPositionChange;
         }
    
       private void OnCamPositionChange(Vector3 camRotation){
              transform.LookAt(position + camRotation * Vector3.forward, camRotation * Vector3.up);
       }
 } 

Hope this helps!

Save the last position from each frame and check the last position against the current one.

Vector3 oldCamPos;

if(oldCamPos != mainCamera.transform.position)
{
mainCamera.LookAt(where ever);
}

oldCamPos = mainCamera.transform.position;

I can suggest you events. Create an event to detect your camera position change and register your object’s script to that event.

//Attach this to main camera and detect position changes here
 public class CameraPositionChange : MonoBehaviour {
 
     public delegate void camPositionDelegate(GameObject cam);
     public static event camPositionDelegate camPositionChangeEvent;

     private Vector3 lastPosition;

     public static void CamPositionChange(GameObject cam) {
         if (camPositionChangeEvent!= null) {
             camPositionChangeEvent(cam);
         }
     }

    void Start(){
        lastPosition = transform.position;
    }

    void Update(){
          if(transform.position != lastPosition){
                 lastPosition = transform.position;
                 CamPositionChange(this.gameObject);
          }
    }
}

//this is your already existing script that you attach to target object. 
public class YourAlreadyExistingClass : MonoBehaviour {
         private SpriteRenderer sRenderer;
         private Vector3 position;
         //private Camera mainCamera; //you won't need this anymore
         private void Awake()
         {
             //mainCamera = Camera.main; //you wont need this line too
             position = this.transform.position;
             sRenderer = this.GetComponent<SpriteRenderer>();
         }
         
        private void OnEnable() {
             CameraPositionChange.camPositionChangeEvent += OnCamPositionChange;
         }
     
         private void OnDisable() {
             CameraPositionChange.camPositionChangeEvent -= OnCamPositionChange;
         }
    
       private void OnCamPositionChange(GameObject go){
              transform.LookAt(go.transform); //with this line each item having this script will look at the camera as soon as it's position change
       }
 } 

Hope this helps!