Camera shake script that works on a moving camera

Hey everyone,

So I came across this camera shake script which works perfectly until you add it to a moving camera which of course breaks it. I was wondering if anyone has done this before and what would be the best way to do this as I don’t particularly want to get the cameras position every frame.

Here is the code:

public class CameraShake : MonoBehaviour {
    public float shakeStrength = 5;
    public float shake = 1;
   
    Vector3 originalPosition;
   
    void Start()
    {
        originalPosition = transform.localPosition;
    }
   
    void LateUpdate()
    {
        if(Input.GetKeyDown(KeyCode.Q))
        {
            shake = shakeStrength;
        }
       
        Camera.main.transform.localPosition = originalPosition + (Random.insideUnitSphere * shake);
       
        shake = Mathf.MoveTowards(shake, 0, Time.deltaTime * shakeStrength);
       
        if(shake == 0)
        {
            Camera.main.transform.localPosition = originalPosition;
        }
    }
}

Thanks

Use the same script, but put it under a parent transform. Anytime you move the camera, move the parent instead.

Oh yeah that’s so simple. I did try something similar but i was still moving the camera and not the parent. Thanks StarManta