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