- Home /
The question is answered, right answer was accepted
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);
             }
         }
     }
Answer by mchts · Apr 11, 2019 at 02:16 PM
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!
Thank you for your help @mchts It works as I wanted!
You're welcome. Could you please accept the answer as well :)
Answer by Nivbot · Apr 14, 2019 at 11:38 PM
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;
Follow this Question
Related Questions
Attaching camera to instantiated object 1 Answer
make player move in camera direction 1 Answer
Smoothly Move camera while looking at object 0 Answers
LookAt in opposite direction? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                