Easy Cinemachine Built-In Screen Shake - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today i'm going to be showing you how to use cinemachine's impulse source so that you can shake your screen when you collide with objects and you can also shake your screen for certain events and this is all built in to cinemachine all right so let's start so the first thing you want to have is cinemachine installed so go to window package manager and be sure to select unity registry to show all the packages and then you want to make sure to scroll down and find syn machine or you can type it in in the search bar up here and just install that right here down here it says install just click it and it will install that package and then we can add a cinemachine camera to follow our player and so you can do that by going to the top where it's a cinemachine and then there's a bunch of different kinds of cameras that you can use in this case we'd want to go for the create a 2d camera since this is a 2d game and in a previous video which i'll link down below i explain more of the cinemachine parameters that you can find when you add in a cinemachine virtual camera to your scene so if you're interested in checking that out it'll be in the description so for your camera the main thing that you want to specify is your follow so you want to follow your player so you can just drag in your player from your hierarchy into that follow parameter and it will automatically center it so that your player is in this yellow square and then there's a bunch of settings that you can specify here i'll just fill them out and show you them all right so here are some of the values that i've picked you can preview this by going to your game tab in window general game and then you can be sure to press game window guides and that'll show you the preview of how your camera is going to be centered and so here are the settings that i chose the main thing is that i increased the x and y damping i added a little bit of look ahead time so it looks ahead to where it thinks the player is moving and then i change the camera distance so it'll be a little further away from the player and then there's some soft zones here if you want more details on this you can check my previous video so you can see how this looks like when you press play it follows it pretty smoothly and you can play around with these values so the main thing that we want to do in this case is that when we crash into this player we want it to shake the screen and this can be used for any kind of game it doesn't have to be 2d it can also be 3d you can easily adapt it for 3d so let's start by adding the collision shake so in the same machine virtual camera if you scroll down you can see there's an extension and you can click that extension and a bunch of stuff will pop up there are extensions for the sun machine like a confiner which confines the spaces which it can move on but in this case we want to add in a impulse listener and so an impulse listener basically listens for impulses from the scene and if it detects an impulse then it will tell cinemachine to go crazy and shake depending on how you told it to shake so there's some options here first of all there's a channel mask so this is kind of like layers for cinemachine so you can have several layers for different impulses so if you want an impulse impacting one layer and not the other you can specify that but in this case we just want the default and you can specify it by going to the edit on the side and it will tell you to make a new cinemachine impulse asset and you can just save that and when you click it and expand it in the inspector you can see that you have a size and you can just increase that size to maybe three and then you can name the channels that you want or layers i'm just going to delete that since we don't need that for this video next up is the gain which is the strength of the impulse signal by the way you can hover over these to see their tool tips and they explain what they do so it's from zero to one in this case one is the normal strength and if you put it to zero it completely mutes the signal so it won't shake at all and then in this case we do wanna check use 2d distance since we are a 2d game and it basically ignores the z-axis for calculations which we don't need so once you've added the listener we actually need a source for the impulse because even if you have a listener if you don't have anything that it needs to listen to then it's basically just sitting around so i've made up a little enemy here but it's not really an enemy he's just a a guy walking about but we'll just say he's an enemy and so there's a couple things i've added to him it's the sprite render of course an animator so he can move up and down and change the sprite i've also added in a box collider so this is very important so in this case we're going to be adding impulse on collision so for collision we need to make sure that our player has a collider so we can click on our player and we can just add in a box collider 2d so we got to make sure that our player has a collider and then we have to make sure that the thing we're colliding with has a collider so i added in a box collider 2d for this enemy as well and you can select is trigger or not in this case it will work for both is trigger if you have it checked your player will go through the object but if you don't have it checked then the player will collide with the object and the object will be like kind of a wall stopping the player from continuing so i'm going to deselect it since generally you don't move on top of people in real life and then one thing that you need to add is a rigid body 2d so it can detect the collision so in this case i've added it to the enemy and there's a couple things that i changed here the first one is that i froze its rotation on the z-axis since we're in 2d we don't want it to be rotating on the z-axis because that's for 3d rotations and you notice that for 2d unity generally has gravity pointing down so if you left it like that it would just fall down because it's a 2d game and it thinks that gravity is pointing down so we can actually change that in the settings for a top down game and so we can just go to edit project settings and under physics 2d just make sure to set the y to zero by default it has y as one meaning the gravity is in the y axis um but in this case we don't want any gravity affecting our player so we can just set that to zero and so once you have the rigid body and the box collider we can add in an impulse source so we can add component and search for a syn machine impulse source there's two of them so we have the collision impulse source and the impulse source in this case we want the collision impulse source since this will be on collision and it handles it for you automatically you don't have to code anything which is amazing and then there's a couple of settings here that we can mess around with in order to change how it shakes the screen so the first thing that you may notice is the impulse channel which as i mentioned before is the layer in which the impulse is happening on and you can change that around depending on how you like and then one big important setting here is the raw signal so this is how we want our signal to move around or kind of like the noise of the screen shake and to make one of these we can just do it in our project folder so we can make a new folder called cinemasheen noise or you can make it whatever you want and then there we can make a new cinemachine noise so you right click create and then under cinema machine there's two settings that we can change there's the noise settings and there's the fixed signal definition let's do noise settings and once you click it you can see in the inspector you have some parameters here for the noise settings it's more if you want to have waves so in this case we have the position noise which is how we want the screen shake to change in position and then we can also do rotation noise which is how we want the screen to rotate during the shake in this case let's just do position since we're in 2d mode we don't really want any rotation but you can just specify it and it will be the same process so let me show you how to do the position noise so we can expand the positions like so in this case we don't want position z since we don't really want to move it in the z axis but of course you can so we can expand position x and position y and you can see that we have components here and you can add to a list and so once you add to a list you have two parameters here we have a frequency and an amplitude a frequency is the speed of the wave and the amplitude is how high that wave goes so let's put the amplitude to one and you can see we have a straight line here and then let's put our frequency to five and you can see this isn't actually linear and it's because there's a check box at the end of this and it says non-random wave if checked so if we leave this unchecked it adds some randomness to the wave which you might like for certain parameters but in this case to show you how it works i'm going to check it and now it will remove the randomness that it adds to it since we have a frequency of 5 it happens much more often if we decrease the frequency it decreases the speed of the wave the amplitude is the noise of the channel you can see in the tooltip here it says larger numbers vibrate higher so this is kind of like the strength of the vibration and you can play around with those values and what's cool is that you can add multiple components to a position so you can add the plus sign here and you can add in another wave so let's say we want our frequency for this one to be a little bit higher so let's say we want a 7 and we want the amplitude to be 0.3 so this actually adds on to this frequency and amplitude it basically combines them together and you can select it to be not random but in this case let's add a bit of randomness up here you can see that we have a preview time and a preview height so we can actually press animated to see a sample of how this screen shake would look like and then we can change the preview height which changes the height of the preview and we can change the preview time which of course the more time you add the slower it will look because it's a much larger time frame and this is just for preview doesn't actually impact the screen shake so let me uncheck this so to add a bit of randomness let's add in our position.y so let me show you what unity says in their documentation they say impulse assumes that the main direction for an impact is down so as a general rule your signals should put more vibration along the y-axis so generally they recommend adding more impulse to the y-axis than to the x and so you can copy these values if you want and the same goes with position z rotation x and rotation y it's the same process and this is really simple to do then there's another kind of signal you can do by right clicking create cinemachine fixed signal definition and so this is with actual curves instead of noise so you can click on the box here and it'll come up with a curve so they have some preset curves that you can pick and this defines how you want the shape to behave over time in this case we want it to start off really strongly then we want it to linearly decrease down to zero you can also make your own curve by clicking this gear icon and pressing new and you can name it noise and you can actually move around the points that you want to wherever you want it to add points you can double click on the line to add a point and then there's going to be two points connected to that line which defines the slope near those points and you can right click and delete key as well and you can do this for the y curve i'm just going to select one and for the z curve in this case we don't really need a z curve all right so once you've defined your noise we can go back into our enemy so we can just drag in our noise settings or your fixed signal and so let me explain some of the settings here that are in the impulse source so the amplitude gains defines the strength of the signal so it starts at zero zero meaning it has no strength one is the default value and if you put it greater to one that means a stronger value so instead of changing the noise settings here you can adjust the overall amplitude and frequency for your raw signal so the randomized checkbox down below randomizes the raw signal start time so it can have a little bit of variability when you start the screen shake time envelope controls the duration of the impulse and the intensity of it so there's two main properties here one is attack and the other one is decay so attack is the lead up to the screen shake and the decay is the end of the screen shake after the the height has been reached it decays off so you know how when a music is playing it doesn't generally start off really strongly it starts off slowly and builds up to the top and then once it reaches the top and the music is ending it kind of soothes away slowly and so for that you can use a curve and they have several curves defined here so we can use this one generally you want it to point upwards because we want it to start off slowly and then increase over time and for the decay we can do the opposite we can specify one going downwards with a slope the sustain time is the time where it maintains the full amplitude after the attack so this is the highest point of impact here you tell it how long you want that to last and so they have a scale with impact check box right here and this means the stronger the impact is the longer it will last so you can check that there then we have a spatial range section this area is mostly to do with the location of the impact and how you want it to affect a larger radius so for example here we have the radius of the impact currently it's 100 which is very large and then we can do a direction so in this case it's fixed so there will be no rotation when there's an impact but if you put rotate towards source it will rotate towards where the impact came from so this is much better for 3d games say you have an enemy or bullet coming from the right and it hits you on your right side then the screen shake will kind of rotate a little bit to the right to indicate to the player that hey someone's shooting you from the right maybe you want to look over there and do something dissipation mode is what happens when the impulse reaches outside of the impact radius you can do an exponential decay soft decay or a linear decay the dissipation distance is well the distance that this kind of goes into effect if you put linearly for example then it will decrease linearly over 1000 distance and finally the propagation speed specifies the meters per second the speed at which the impulse propagates through space so right now it has a 343 meters per second speed which is very fast which means that the higher the speed is it will allow listeners to react more instantaneously while if you put it lower it will take listeners a little bit longer to react and by listeners i mean the cinemachine impulse listener attached and then we have a trigger object filter so this is really important so this is what we want to listen to in what layer mask and with what tags so in this case we want our layer mask to be player and we can just remove the default because we want it to detect collisions with the player and you want to select your player and up here under layer you want to be sure to select player if you do not have a player layer it's because you have to add the layer and i just added in a player layer into number eight so make sure your player has that layer and then the enemy has the layer mask and you can also choose which tags you want to ignore lastly we have how to generate the impulse so you can use the impulse direction so depending on the direction of the impact the impulse will be impacted um you can scale impact with mass which they take this from the rigid body and you can scale impact with speed so how fast the objects are colliding in this case i'm not going to select any of those so right off the bat if you press play and test it out then we have this intense shaking screen whenever we collide with the player and this is all out of the box no coding whatsoever which was pretty amazing and you can just go into the enemy and actually decrease the amplitude to like 0.2 and we can decrease the frequency to maybe 0.4 and see now the effect is much less and then finally what if you want to do an impulse signal but you don't want to actually do it on collision maybe you want to do it when a certain event happens well that's really easy so i'm just going to right click and create an empty object to show you how it would work i'm just going to call this shake and so i've already made a script for this called shockwave unity event which i can show you it right now so as you can see i just have a public unity event you can work with events and then i have a simple invoke repeating so after three seconds we will execute this function right here and this function will execute after that every four seconds so after every four seconds we want to invoke our unity event so now we have an event and then we can add in a new component and we can add in a normal cinemachine impulse source which is the same settings as the other one except in this case it's not collision it's just a normal impulse that we have to manually tell it to shake so i can just add in my noise here i'm just going to decrease the amplitude and frequency once again and right here you can see that in our unity event we can add in a new event and we can just drag in the script here and then under no function we can say cinemachine impulse source and then we can generate the impulse but this is a great example if you had these in separate scripts you can just drag in the script here and call it through a unity event if you don't want to call it through a unit event and you want to call it manually you can also just have a script for that as well so i have a script called shockwave listener i just say using cinemachine and then i have a reference to the cinemachine impulse source in the awake function i get the component attached to this game object the cinemachine impulse source and then i do the same thing as the other one i do an invoke repeating on this function and then i call source dot generate impulse there's a couple other overloads for this function you can see that we have three overloads so we have one that takes in a vector3 velocity so this is the velocity of the impulse if you collide with something you can give it the direction it collides with and move it in that direction then we can add in a float force and finally we can just add in nothing and it will just use the pre-existing settings that it has to generate that impulse so this is if you want to call it from the script directly so let's use the unity event one and when we click play you will see that it starts to shake every four seconds so this is great if maybe you have an earthquake or maybe something's happening in the scene maybe there's a big boss that's stepping down every time the boss walks you want the screen to shake and you can send an event to the impulse source that impulse source will get that event and it will propagate that event over to the impulse listener and then cinemachine will be like okay i have this information about the source i have how much i want the screen to shake so then i will shake the screen right now so that's kind of how it works so yeah that's how to do a shake screen you don't really need any coding whatsoever but if you do want to do events and stuff you can just easily call the impulse source with one line so yeah i hope you enjoyed thanks so much for watching and thanks to all my patreons if you're interested the link is in the description additionally i have a discord channel where you can ask for help or you can just chat so thanks so much for watching and see you next time
Info
Channel: samyam
Views: 22,163
Rating: undefined out of 5
Keywords: unity cinemachine screen shake, cinemachine, camera shake, screen shake, cinemachine shake, cinemachine no coding shake, screen shake in unity, unity screen shake, unity screen shake effect, unity camera shake cinemachine, cinemachine impulse, unity cinemachine impulse, unity shaking screen, unity shaking, cinemachine shaking, unity shake screen, cinemachine noise, random noise, cinemaching random noise, unity shake effect, impulse listener, impulse source, built in shake
Id: 8XZqopmI6AI
Channel Id: undefined
Length: 18min 26sec (1106 seconds)
Published: Thu Aug 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.