Geek Culture
Published in

Geek Culture

Improving the Camera Shake with an Animation Curve!

Starting from my last post, the effect currently has the same strength throughout the duration. I’d like to extend this by making it start off mid-strength, then go full strength, then slowly go to zero strength over the full duration.

There are a number of ways I could do this, but I will use something very useful called an “Animation Curve”. It is a variable type you’ve probably seen before in Unity animations.

An AnimationCurve variable, shown in the Inspector

This allows me to set a Y value based on an X value. You’ll understand this more in a moment.

First, I’ll add a variable to the script.

And add a line to the loop which will multiply the random offset by the Animation Curve’s Y value based on an X value.

Evaluate()” returns the Y value from the curve when given an X value as a parameter. So I need to pass in the current “_durationTimer” as the X.

But there’s a problem!

My effect duration is not constant. It is set when I call the StartShaking function. So unless I only ever set duration to 1, it will cause an error because the Animation Curve (by default) goes from 0 to 1.

The solution is to convert the “_durationTimer” and “duration” to be between 0 and 1, regardless of the original duration.

This converts a value in a ?->? range into a value in a 0->1 range

Now I can set the Animation Curve to be whatever shape I want, and the offset strength will reflect that whenever the effect plays.

Change the Y value along the X value

After some tweaking, we get this:

It’s a bit hard to see, but the effect strength gets lower as the effect goes on

Looks great! Hope you liked this feature guide!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Taylor

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.