Delaying a Billboard LookAt rotation w/C# (WaitForSeconds?)

I’m trying to take code for keeping a billboard constantly facing the camera and add in a delay to the rotation so that if the camera moves, there’s a momentary delay before the billboard reorients itself to face the camera (briefly exposing the 2D illusion).

Using WaitForSeconds seemed like the right approach, but after following a few examples of how to implement WaitForSeconds, the target billboard still keeps up with the camera without delay.

using UnityEngine;
using System.Collections;
 
public class CameraFacingBillboard : MonoBehaviour
{
    public Camera m_Camera;
 	void Start()
	{

	}

    void Update()
    {
		StartCoroutine (MyMethod ());
		Debug.Log("Transforming");
		transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.down, 
		                 m_Camera.transform.rotation * Vector3.back);
	}


	IEnumerator MyMethod() 
	{
		Debug.Log("Waiting 2 Seconds");
		yield return new WaitForSeconds(2.0f);
		Debug.Log("After Waiting 2 Seconds");
		yield break;
	}

}

You need to modify the transform in your coroutine. YieldForSeconds won’t stop the calling function, just the execution of the coroutine. Something to keep in mind is that any code you want executed by timing in a coroutine must be in that coroutine, after your WaitForSeconds call.

Try:

using UnityEngine;
using System.Collections;
  
public class CameraFacingBillboard : MonoBehaviour
{
    public Camera m_Camera;
    private Vector3 m_lastCameraPosition;
    private Quaternion m_lastCameraRotation;

    void Start()
    {
        m_lastCameraPosition = m_Camera.transform.position;
        m_lastCameraRotation = m_Camera.transform.rotation;
    }
 
    void Update()
    {
        if (m_lastCameraPosition != m_camera.tranform.position ||
            m_lastCameraRotation != m_camera.transform.rotation)
        {
            Debug.Log("Camera changed position or rotation, changing billboard in 2s");        
            StartCoroutine(MyMethod());
        }
        m_lastCameraTransform = m_Camera.transform.position;
        m_lastCameraRotation = m_Camera.transform.rotation;
    }
 
 
    IEnumerator MyMethod() 
    {
        Debug.Log("Waiting 2 Seconds");
        yield return new WaitForSeconds(2.0f);
        Debug.Log("After Waiting 2 Seconds");
        transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.down,
                        m_Camera.transform.rotation * Vector3.back);
        yield break;
    }
 
}

Note that this won’t work well if the camera is often changing position or rotation. If it does, you should check if the coroutine is running and if so, stop it before starting it.