Search Unity

Question Face the Camera, Object.

Discussion in 'Timeline' started by adamplusplus, Oct 22, 2022.

  1. adamplusplus

    adamplusplus

    Joined:
    Jan 31, 2018
    Posts:
    2
    My project uses a simple billboard script to cause objects to face a target camera.

    The issue occurs when the target camera jumps from one position to another. Upon arriving at the new position, a frame is sometimes rendered with the objects facing the previous position. This manifests as a flicker when viewed in real time.

    The behavior in real time:

    Scrubbing through the third transition where the behavior occurs:


    The billboard script is extremely simple, and is place on any object whose y axis rotation is dependent upon the camera. The same result occurs whether it is in update or fixed update.
    Code (CSharp):
    1.     void LookAtOrient()
    2.     {
    3.         var cameraPosition = cameraTransform.position;
    4.         cameraPosition .y = transform.position.y;
    5.         transform.LookAt(cameraPosition );
    6.     }
    7.  
    Any ideas on how I could solve this?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Sounds like a script execution order problem. You have to make sure that your billboard script executes this code after the camera placement has occurred.

    If you're using Cinemachine, then the camera is positioned by CinemachineBrain.LateUpdate(). You have 2 options:
    1. Orient your billboard in LateUpdate and set its script execution order to be after CinemachineBrain, or
    2. Add a listener to CinemachineCore.CameraUpdatedEvent, and orient the billboard there.
     
    soso8168 and adamplusplus like this.
  3. adamplusplus

    adamplusplus

    Joined:
    Jan 31, 2018
    Posts:
    2
    Thank you for your diagnosis and suggestions Gregoryl!

    Your first proffered solution works! I will stress test it later when I get a chance.

    For anyone else that might use this advice:

    Changing script execution order: https://docs.unity3d.com/Manual/class-MonoManager.html
    Script example
    Code (CSharp):
    1.    
    2. [ExecuteInEditMode]
    3. public class Billboard : MonoBehaviour
    4. {
    5. //...
    6.     [SerializeField]
    7.     Transform targetTransform;
    8.     private void LateUpdate()
    9.     {
    10.         LookAtOrient();
    11.     }
    12.  
    13.     void LookAtOrient()
    14.     {
    15.         var target = targetTransform.position;
    16.         target.y = transform.position.y;
    17.         transform.LookAt(target);
    18.     }
    19. }
    20.  
    Thanks again Gregoryl!
     
    Gregoryl likes this.