Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 15, 2019 at 08:53 AM by ratchetunity for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ratchetunity · Apr 11, 2019 at 01:43 PM · cameratransformlookatlookmaincamera

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);
             }
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ratchetunity · Apr 15, 2019 at 08:54 AM 0
Share

Thank you for your help @mchts It works as I wanted!

avatar image mchts ratchetunity · Apr 15, 2019 at 09:04 AM 0
Share

You're welcome. Could you please accept the answer as well :)

avatar image
0

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;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

187 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

An alternative to RotateAround for collisions 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges