CAMERA SHAKE in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Wouldn't it be a problem if multiple sources called the camera shake? Then it would set the original position to what it was when the camera shake was called, and possibly reset the position to some weird position due to another camera shake taking place.

👍︎︎ 10 👤︎︎ u/[deleted] 📅︎︎ Feb 27 2018 🗫︎ replies

Just use Cinemachine, it already has shaking in there.

👍︎︎ 3 👤︎︎ u/SuperButek 📅︎︎ Feb 27 2018 🗫︎ replies

Hey brackeys!

👍︎︎ 4 👤︎︎ u/coltenbrown 📅︎︎ Feb 27 2018 🗫︎ replies

An artist asked me to add camera shake recently, so I've just done this. I'm not a huge fan of this solution. If several things call the camera shake at once, it will mess up the original position, something that will also happen if the main camera is disabled and reenabled for any reason during the shake (because that kills the coroutine and the camera will be in the "shaken" position), and it's very hard for a designer or artist to invoke camera shake like this. Also, as he mentioned, there's no way to do fades or things like that.

The solution I went with, which solves virtually all those problems, is to do something like this:

public class CameraShake : MonoBehaviour {
    public float Magnitude = 0.0f;
    //... other shake options might follow here...

    void LateUpdate() {
        // do the camera shake, according to magnitude. 
    }
}

Essentially, the camera is "always" shaking, but with magnitude set to 0.0, it's just standing still. This basically solves all of the problems: one shake at a time, no losing track of the camera's origin (even if it's disabled or even assemblies reloaded), and it has a HUGE bonus: since the magnitude is just an exposed field like anything else, it can be animated. An artist or designer can tightly control how much the camera is shaking, how it fades up, down, everything, using either regular Animators or the new Timeline.

It's a good tip if you're aspiring to be a Unity developer: at some point, you're going to work with (and for) people who aren't programmers. If you're making a feature, think about how you can get them to use it, through things like UnityEvents, Animators, Timelines, whatever. Also, don't use coroutines if you can help it :)

My solution was slightly different than this to suit our game, but this was more or less the gist. Worked really well.

👍︎︎ 2 👤︎︎ u/hi_im_new_to_this 📅︎︎ Feb 27 2018 🗫︎ replies

Just watched this last night.

👍︎︎ 1 👤︎︎ u/Chesnekov 📅︎︎ Feb 27 2018 🗫︎ replies

Oh nice! Haven't watched this yet, but if I crash horizontally into a wall in a 2D racer, can I set the vector to the direction of the impact? That's something most camera shake tutorials miss. They just apply a generic noise which is not great.

👍︎︎ 1 👤︎︎ u/YummyRumHam 📅︎︎ Feb 27 2018 🗫︎ replies
Captions
a great way to add some extra oomph to a game is by using camera shape whoa it's great for emphasizing action and making your camera feel more part of the world so in this video we'll go over how to make your own camera shake and towards the end I'll show you how to set up an awesome tool that I personally use to shake things up get it because of the shape also this video is sponsored by Dennis pen tutor dennis is an awesome game dev tutor who's just launched a new course on udemy the complete unity 3d game development course by taking the course you'll learn how to code in c-sharp make basic 3d assets using blender as well as get a solid understanding of unity when you're done you will have created six different games from pong to fumble you'll also learn how to monetize your game using unity hats if this sounds like something you're interested in simply click the link in the description to get started and get a discount also special thanks to Sean Carey Diego guy kudamon dying guy in Murphy o infinity PBR your io mer and hands off tuned for their support on patreon alright let's get started so here's the scene in unity where every time I click with my mouse and explosion appears but of course this could look way cooler if we added some camera shake to do that that's select our main camera and let's add a new component to this called camera shake let's say new script select C sharp and hit create an ad now we can go ahead and delete both of the methods here and instead we want to create our own one but we don't want this to be a normal function we want this to be a cover of team if you've never heard about Kuro teens before I suggest checking out the link in the description for now we're just gonna power through whenever you're working with Kuro teens you want to make sure to have using system dark collections and instead of writing void at the beginning of her function we now write I in numerator then we follow it by the name of our function which we'll just call shake and our shape function is going to take in two parameters one for the duration of the shake and one for the magnitude in other words the strength of our shake and the first thing that we want to do is store the original position of our camera before we start shaking so that we can reset it once we're done we'll create a vector3 for this and call it original position and size equal to transform that local position and then we want to create a while loop because we want to keep shaking for X amount of seconds specified by our duration so we could go ahead and create a float called elapsed this is going to keep track of how much time has elapsed since we started shaking so by default we'll set this to zero then we want to keep shaking as long as while elapsed is less than the duration so in other words as long as the amount of time that has gone by since we started shaking doesn't exceed the duration well then we probably want to keep shaking and now a bunch of different ways to calculate the offset you want to apply to your camera in order to make it look like it's shaking but it can actually be done in an extremely simple way we're simply going to be offsetting it by a random amount on the X&Y every frame so first we'll calculate the X so float x equals a random range between negative 1 and 1 and we can then multiply this with our magnitude so the greater the magnitude the more will offset our camera and we'll do the same thing on the y so float y equals random that range between negative 1 and 1 and we'll multiply it again with the magnitude then we simply need to apply this offset to our cameras position so we'll go transform local position equals a new vector3 and here we'll give it our X or Y and you could definitely offset it on the Z as well but in our case I don't think that's necessary so we'll simply use original position dot Z and this is kind of where the magic of the kuroh teen comes in because we want this while loop to basically be running alongside our update function so that every time the update function runs we go through and run this code once as well to do that we use a single line of code we write yield return no this basically means that before we continue on to the next iteration of the while loop we want to wait until the next frame is drawn again the syntax might be really weird if you've never encountered a current team before but that's basically what is happening also we of course want to make sure to update our elapsed time so right before we wait for the next frame we are going to write elapsed plus equals time Delta time so every frame we increase this value by the amount of time that went by and so this is going to keep running until we exceed our duration at which point we're going to get to this point in our code and we can go ahead and reset our position so we'll go transform dot local position equals our original position and there we go we've now created a crow routine that is going to shake our camera of course we want to be able to access this from another script in order to trigger it so we'll go ahead and mark it as public then we can go back into unity we should see we have no errors here we can go to the point where we want to trigger our camera shake in my case that's under my explosion trigger this is just a very simple script that every time I click with my mouse is going to play this particle effect so let's open up the script and as you can see this is exactly what it's doing we reference the explosion we check if we can Emmaus button down and if we do we press play on the explosion so just like we reference our particle system we also need to reference our camera shake so public camera shake and we'll just call it camera shake and then right after our explosion starts playing let's go camera shake dot and then we can simply call this shake function however here's another thing that you need to remember when working with kuroh teens and that is whenever we want to start a kuroh teen we can't just call it like a normal function instead we have to go and write start co-routine and we then pass it the function that we want to start like this and now we can simply feed it the duration I'm gonna set that to 0.15 and the magnitude I'm gonna set that to 0.4 and now if we save this go to unity and drag in our main camera and then try to press play we can see that something happens the camera is definitely shaking but it also seems to be snapping to another position that's because we're currently just overriding the position of our main camera and our main camera might have some position in the world that we don't just want to ignore so what we do instead is create an empty game object that is basically gonna be the anchor of our camera so we'll go to the hierarchy right click on our camera and hit create empty I will call this our camera parent or camera holder and make sure to reset the transform here so it's at the same position as our main camera we'll then drag this out and instead take our camera and parent it to the camera holder so now our main cameras position should always be 0 0 0 and then when we want to actually move our camera we instead move the camera holder this means that we can go ahead and over the position of our camera as much as we want it's still going to stay in the same general area under the camera holder so if we now hit play we can see our camera shake is working awesome so let's say simple and quick way to do a camera shake by yourself but it has some limitations it doesn't give too much control over this shake it doesn't rotate the camera in any way there's no fading in and out of the shake and there's no control over roughness or frequency it's always moving the camera every frame and so if you want smoother shaking this is not the way to go and also having to reference the camera shake every time and you start cover teens seems a bit clunky so if you want to get serious about your camera shake I recommend going to the asset store and picking up easy camera shake it's completely free and I'll of course have a link for it in the description I'm gonna go ahead and hit import hit import once more I should now see a folder in your project called easy camera shake only we have some documentation the different scripts as well as a demo scene that you can check out in our case I'm just gonna go to the game view here I'm gonna select our camera and get rid of the camera shake and instead I'm gonna go under the ICI camera shake and add the camera shaker here we can choose how much we want this shake to influence both our position and rotation for now we can just leave these values as is and the cool thing is that if we now go into our explosion trigger we can get rid of this variable at the top I'll go ahead and delete this piece of code as well instead we'll go to the very top and say that we want to be using easy camera shake and now all we need to do is go camera shaker dot instance and then we can do dot shake once and this is going to shake the camera once with all the values that we now specify first we're gonna give you the magnitude imma said that too for we can give it a roughness as it says here roughness of the shake lower values are smoother higher values are more jarring I'm gonna set this to forest well since we are working with an explosion here we then set the fade in time I'm gonna set that to 0.1 and a fade out time and I'll set that to 1 and if we save this and play well now we now have a much smoother camera shake that is both easier to use and allows for a much more in-depth control pretty cool right now I guess all we need is some sound effects that's pretty much it for this video if you make sure to subscribe so don't miss a future one also make sure to check out the complete unity 3d game development course that's something in the description on that thanks for watching and I will see you in the next video thanks to all the awesome patreon supporters who donated in January and a special thanks to Sean Kerry Diego guy kudamon Diane Kyne passio infinity PPR yo-yo ma Sybok mommy Derek Eames Kirk Marissa Murphy Peter die John Ramirez double tap 45 James P Superman the great John Burkhart Jason the Tito Alex Akatsuki Bien Phu Club Sunni Arabs and James Rogers Robert punt Rob Ferran and Erasmus
Info
Channel: Brackeys
Views: 396,543
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, series, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, shake, camera, camera shake, position, movement, earthquake, explosion, explosions, turbulence, chaos, EZ, EZ Camera Shake, Shaker
Id: 9A9yj8KnM8c
Channel Id: undefined
Length: 9min 28sec (568 seconds)
Published: Sun Feb 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.