Camera Shake while following Player?

Currently I have this script attached to an empty game object which follows the player around. The camera is also a child of this object.

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
    public float duration = 0.5f;
    public float speed = 5.0f;
    public float magnitude = 0.2f;

    void Update()
    {
        if (Input.GetKeyDown("f"))
        {
            PlayShake();
        }
    }

    //This function is used outside (or inside) the script
    public void PlayShake()
    {
        StopAllCoroutines();
        StartCoroutine("Shake");


    }

    private IEnumerator Shake()
    {
        float elapsed = 0.0f;

        Vector3 originalCamPos = transform.position;
      
        Debug.Log(originalCamPos);

        float randomStart = Random.Range(-1000.0f, 1000.0f);

        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;

            float percentComplete = elapsed / duration;

            float damper = 1.0f - Mathf.Clamp(1.5f * percentComplete - 1.0f, 0.0f, 1.0f);
            float alpha = randomStart + speed * percentComplete;

            float x = Mathf.PerlinNoise(alpha, 0.0f) * 2.0f - 1.0f;
            float z = Mathf.PerlinNoise(0.0f, alpha) * 2.0f - 1.0f;



            x *= magnitude * damper;
            z *= magnitude * damper;

            x += originalCamPos.x;
            z += originalCamPos.z;


            transform.position = new Vector3(x, originalCamPos.y, z);

            yield return 0;
        }

        transform.position = Vector3.Lerp(transform.position, originalCamPos, Time.deltaTime * 5f);
    }
}

However, as the player moves, while the camera is shaking, it doesn’t follow the player. It only updates its position after the shake has ended. I know the error is when grabbing the original position and lerping to that after the shake, but I’m not sure how to update that to the player’s position. Any thoughts?

Im not sure why you are doing this in a co-routine. It would be much simpler and less obtuse just to have a shake component that jitters the camera in its Update.

BUT doing it this way you will have to acquire the camera’s current position at the start of the while loop because thats where the code restarts after your yield.