Skip to content

Instantly share code, notes, and snippets.

@insominx
Last active November 23, 2021 23:04
Show Gist options
  • Save insominx/f3acce17108d0a2e4619 to your computer and use it in GitHub Desktop.
Save insominx/f3acce17108d0a2e4619 to your computer and use it in GitHub Desktop.
IEnumerator Shake() {
float elapsed = 0.0f;
Vector3 originalCamPos = Camera.main.transform.position;
while (elapsed < duration) {
elapsed += Time.deltaTime;
float percentComplete = elapsed / duration;
float damper = 1.0f - Mathf.Clamp(4.0f * percentComplete - 3.0f, 0.0f, 1.0f);
// map value to [-1, 1]
float x = Random.value * 2.0f - 1.0f;
float y = Random.value * 2.0f - 1.0f;
x *= magnitude * damper;
y *= magnitude * damper;
Camera.main.transform.position = new Vector3(x, y, originalCamPos.z);
yield return null;
}
Camera.main.transform.position = originalCamPos;
}
@jimtang
Copy link

jimtang commented Jun 6, 2013

should position be calculated like this?
... = new Vector3(x + originalCamPos.x, y + originalCamPos.y, originalCamPos.z); // line 20

@insominx
Copy link
Author

yes it should. this trivial example just has the camera at the origin which allows me to simplify a bit. However, if the camera was positioned somewhere else, you'd need to account for that like you suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment