How to make a Sniper Scope Effect - Unity FPS Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] and I want to welcome to this video in creating a sniper scope effect this what we'll be creating today it's a really cool effect and it can look pretty complicated but in this video we'll be breaking it down into small tasks that when combined will produce pretty good results on the heater here broke so that's the reason for this silly thing let's just go ahead and get started just assume you're going to be working with today you of course going to need some weapons I'm going to be using the l96 sniper rifle that you see here it's part of the modern weapons bundle which also includes the shotgun and these rifles in this pistol and we'll be adding to it as we go it's created by a guy called matias if you want to download the weapons for yourself you can simply go to dev assets comm and you can select the modern weapons pack choose the price that you want you can even grab it for free if you're just unable to support then simply hit pay what you want and it's going to download as a zip file with a unity package that will set everything up for you so now that we have our weapons in here we are pretty much ready to get started and you can see here if i zoom out that I have a rigidbody FPS controller in my scene you can pretty much use any kind of controller that you have I just grabbed this from the standard assets so if you want to get the same one you simply right click go import package and then characters and it's part of the characters folder here under the standard assets folder on the first person character and I simply grab the FPS controller here so that should set everything up and that just means that if we now hit play inside of unity and remember to put a Collider on your ground plane as well we are able to move around the scene and jump and look around and stuff like that so nothing fancy but just helps us set up a very basic first-person controller so now to actually get this gun mounted on our controller we need to kind of drag it under the main camera so let's just go ahead and duplicate the sniper rifle and let's drag it under the main camera and let's reset the transform on this so you can see currently that it's really offset compared to the camera it's actually inside the camera and that's not really desired so to fix this let's go ahead and create an empty game object under the main camera which we'll call a weapon holder and we'll basically just parent all our guns to this weapon holder object it will simply allow a to line up all our guns at once so that we can easily switch out guns if that's part of your game so now let's take our sniper rifle and parent that to the weapon holder and now we can position everything using this empty game object so first of all we need to have a look at where our first-person control is pointing so it's pointing in this direction and so I can see that we need to at least flip this 90 degrees on the Y and now it points into the right direction here but we also need to kind of move it over to the right a little bit adjust the X movement here and definitely also move it forward a bit and from here we can pretty much fine tune all of our values in the inspector so we'll just drag it down a bit on the Y drag it over a bit more on the X and what I really recommend that you do here is bump up the clipping plane so if we select our main camera you can see that our gun is currently being cut off when it gets too close to the camera the reason for this is that we currently have a minimum of 0.3 units from the camera before we start drawing any models and we can simply reduce this by changing this value to something smaller say point zero two and you can see that it's now rendering the entire weapon so what we can do now is go back to our weapon holder and readjust this so probably want to move it over a tad more maybe down even more something like that looks pretty decent and you should of course spend way more time fine-tuning this stuff than I do I just wanted to get this working quite aa fast and easy for the tutorial so let's just say that we are now are really satisfied with our weapon placement we can go ahead and rename this one to our sniper rifle just to keep things very simple and we have a weapon holder and our main camera so the next thing that we want to do is animate our weapon holder we want to have two animations one for when we are not scoped in so when our weapon is idle and one for when we are indeed scoped in so let's select our weapon holder let's go to our animation tab if this is not open you can go to window animation or press control 6 and let's hit create and is going to allow us to create a new animation and I like to keep all of my animation data within one folder so let's do new folder and it's called this one animation we can double click on that and then we can name our animation accordingly I'm going to rename this to idle and you can see that it's now created an idle animation along with a weapon holder controller and this is what is going to mix together different animations in order to transition from one animation to another and define parameters such as shoot the animation lube and what should happen so this is basically controlling the animations and this is the individual animation so let's first create some parameters for our idle animation you can see currently we have no properties in here so nothing will really happen what I think we should do is go and select that weapon holder if you haven't already make sure you're in record mode and you can see that our play buttons are also now red to signify that we are in record mode I think we should simply animate our Y value a tiny tiny bit up and down kind of like we're breathing so let's start at a y-value of minus 0.12 and then we can move forward let's say 2 seconds and I'm just scrolling here and we can maybe animate this arm downwards by simply adding a 2 onto that and then we can just scroll out even further and we can copy the first keyframes over to the 4 second mark so if we just play this back you can see that we have a very subtle movement on the gun I kind of like that and we can just smooth this out even more by selecting all the keyframes right-clicking and selecting flat and that's going to make sure that if we have a look at our curves here and we of course need to only look at the Y position and hit F here you can see that it is curving a tiny tiny bit but the values here are so small that it's not going to be super noticeable cool so that's our idle animation now we can shift back to the dope sheet and we can create a new clip and this is of course going to be our scoped in animation and here I just want to call this something like a scoped and we can say that and again we want to select our weapon holder and here I want to reset the x-value I want that to be zero so that our gun is totally centered I also want to move up on the Y and you can see that it's moving very quickly so it's hard for me to fine-tune but if I just hold down alt it's going to allow me to do or adjust this value in much smaller increments so something like negative point one looks pretty good and then we can move in the Z a little bit as well to get the gun really close to our camera something like that and you will notice that I've just put this keyframe at the very beginning of the animation I didn't do any kind of transition you may you may think that we should really start here and then as the animation went on we should transition to on this position but instead of just put it at this start and the reason for this is that we can actually have Meccan in the Unity animation system automatically transition between this and this position for us so we don't need to actually keyframe that in and I'm going to show you that in a second so now that we have these two animations what we can go ahead and do is actually open up our weapon holders so this is again the animation controller what controls what animation we're currently playing so let's just double click that and it's going to open it up in the animator and our animator is basically the editor for the animation controller so again which we differentiate between our animation down here where we create animation clips and the animator where we transition between animation Clips cool so and again if the animator isn't open you go to window and you select animator and you're good to go so you can see here it's selected idle as default clip that's why it's orange and we also have a arrow pointing from the entry which means or the beginning of our game over to the idle animation if it's not already you can always right click and set hit set as layer default state and it's going to change that now we need to create a transition from our idle to our scope so let's right click make transition over to the scope but we don't want this to happen as soon as we start the game right now if we start the game it's going to play the idol and then go straight to the scoped we instead want this to happen when a certain condition is met and that is whenever we choose to scope in using some kind of player input to do this we create what is called a parameter you can see up here we have two tabs the layers and the parameters make sure you are under parameter and then hit the plus sign and we're going to select boolean and the boolean here is something like is scoped or just scoped for short and this is going to control whether or not we are currently scoped in so you can see that I can switch this to default to true or to false we of course want this to be defaulted to false so that when we start a game we're not going to start scoped in then on this transition we are now able to add a condition we simply hit the plus sign here and it will automatically add the scoped parameter and say that we only want this to happen when scoped is true and we can do the same thing when transitioning back so we make a transition from the scoped to the idle animation we select the backwards transition and we add a new condition here that we only want to do this when scoped is now false so that is how we transition between animations however there will be a problem with this and that is we are actually going to wait for idle to finish before we transition to the new animation that's what's called exit time and if we go in and click on this transition here you can see that's currently enabled what we want to do is disable exit time and then instead go in here and say that we want this to have a fixed duration where the transition duration is 0.15 I find that to be a pretty good value and that basically means how quickly we are going to scope in we of course want to do this as well when we are transitioning back to the idle animation so here let's select our backwards transition and remove exit time and it's also set the transition duration to 0.15 and you can have different transition durations if you want to scope in quicker than you are scoped out you can simply change the transition duration on the individual transitions but I want them to have a uniform length so to preview what we just made we can take our animator and duct it over here by the inspector so that we can see our game view we can also just scroll over here to see our different transitions by the way to move around the animator I use the middle mouse button or you could also use alt and then left click and basically what we'll do here is select a weapon holder and when we now hit play we should see the different animations playing you can see currently that the idle is playing and how far it is in its animation cycle if we now hit escape our mouth should appear and don't mind the fact that the view here is still following our mouse movement it's not going to matter what we can do now is go up here and we can see what happens when scope is set to true so if we click that it's going to go into transition over to our scoped animation and when I then click it again it's going to transition back and you can see how much it smooths this out and that is all done by making them itself we didn't have to do that at all so now we're ready to control these animations through script and to do this I'm just going to select the weapon hold the object but you can pretty much place it anywhere you can even add all this logic to an already existing script say you have a script called sniper rifle you can just add the scoping mechanic to that but for the purpose of this tutorial we're going to be creating a separate script let's hit add component and let's call the script something like scope let's hit new script create an ADD and let's just don't click this to open it up in visual studio so let's begin by creating a reference to our animator let's remove these two functions and instead create a public animator and we'll also call it animator with a non-capital a what we want to do is access this animator whenever we want to scope in and out and change the value of our scope parameter that we just created so to do this let's get some input from the player and remember we check for input inside of the update method so let's write void update and inside of the update method we write if input get button down and this will return true when the button that we now specify is click and the button that we want to check for is fire 2 and fire 2 is the right mouse button what you normally use to scope in you can of course write something else here but I'm just going to be using that so inside of the safe statement we can now change some values on the animator to do this we go animator dot set bull and we then specify the name of our boolean parameter that was scoped and remember this needs to be exact because it's not going to throw an error if it's not so just to make sure that we're using the same name here we'll go inside of our animator inside of unity and just copy the name here and paste it in it's very important that you get that right then comma and we then input whether or not we want to set it to true or false but in our case that varies on what it's currently set to we just want to flip it so if it's currently true and we're scrubbed in we want to scope out and the other way around if we're not scoped in and it's set to false we want to set to true in order to scope in so to do that let's keep track of whether or not we're currently scoped in on the easiest way of doing this is just creating a private boolean called is scoped and we default that to false you could also just get the value from this bowl but I think it's better to keep track of this in a separate private variable on the script so basically what we do here is just input whether or not we're currently scoped there we go so if we are scoped we set that to true and if not we set it to false but we also need to flip the value of its scoped to do that before we access the animator we go is scoped equals the opposite of what is scoped currently is so again this means that we'll get the opposite so if it's false this will return true and if it's true it will return false you get it the cool thing about this piece of code is that it's so ridiculously small yet we can animate pretty much anything inside the game and it should actually already be working so if we go to a weapon holder now let's go back to the inspector we just need to drag in the animator and we pretty much hit ready to hit play so now when I right-click we scrub in I'm going to right-click again we scoped out the next thing we want to do is overlay some kind of UI that resembles a scope and I've provided this inside of the modern weapons pack as well so if you double click that after downloading it and access the extra folder you can see in here we have a scope overlay PNG and we simply drag this inside of unity you can use any image I'm just going to be using this one we want to our click on it and change the texture type to sprite 2d and UI and we also probably want to bump up the max size to our 4k so let's just hit apply on all that and it's going to very quickly apply it and you can see that it now is transparent within this scope here so let's right click in the higher Crego UI and then image and it's going to create a new canvas for us along with a 2d image let's go to the scene hit F to focus on it and switch to 2d mode and we can go ahead and change the source image here we want to change that to the scope overlay we just import it we also want to hit set native size to scale this up to match its correct proportions and finally you can see in the game that it already looks pretty good but if we maximize the game view here and I'm doing that by shortcut by simply hitting shift space it doesn't really scale up with the screen so let's go into our canvas and change the UI scale mode to scale with screen size let's slide this all the way over to height so that each time our window gets taller it's going to scale with it awesome so that looks a lot better and I'm actually really satisfied with that now let's rename our image to scope overlay so let's now control this through a script so let's disable it inside of the inspector and let's head into visual studio and we can create a reference to the object so let's go public game object to create a reference let's call it something like scope overlay and then we can simply go to the update method here and type scope overlay whoops scope overlay set active and input the is scoped variable however this is going to propose a problem because if we now go into unity and we remember to hook up the scope overlay here so drag that in there you will notice that when we play it's going to enable the image as soon as we start scoping and we actually want some delay on that because we only want it to appear when we have fully scoped in and the animation is done so we want to add a bit of delay to this the way to do that is by creating a separate function that will control on our scope so we'll create a void on scoped and we'll also create another void called on unscoped and then we simply go down here and we say if arm is scoped we want to call on scoped else we want to call on unscoped awesome but of course we want to delay this a little bit and inside of a normal function in c-sharp we're not able to wait a certain amount of seconds to do that we need to convert this into a kuroh team by instead of typing void we type I enumerator and now when we're calling it we don't want to say on scope we want to say start corrode teen and then on scoped there we go so we just need to wrap that in a separate function call and then down here we can now say yield return new waitforseconds and again if you've never seen a KO routine the syntax here is going to be so super weird but just type after me and know that whatever you put in here is the amount of seconds that it's going to wait before calling the rest of the code so in here we'll put point one five the same number as we did for the transition duration then afterwards here we can set our scope overlay oops scope overlay we can say dot set active to true and up here we can say scope overlaid set active false awesome so that should actually work and now we should have delay so that our on transition will finish before showing the scope and indeed it does and you can see just how much cooler that is next up we of course want to disable the weapon when we are scoped in because right now it's really getting in the way and we also probably want to increase our field of view so that we will get the impression that we are actually assuming and then the scope is actually doing something so to do that we simply add a few references inside of Visual Studio here we need a reference to our gun and we could just disable it but one thing that is way more normal to do is have a separate camera rendering the weapon and then have one camera rendering everything else but this will also help you do is solve problems where your weapon is clipping through other objects if I were to go in here now into the scene go out of the 2d mode here and focus on our rigidbody FPS controller if I then go in here create a 3d object cube and place it in front of the controller here if we then hit play and walk into this cube you can see that our gun is clipping through so to solve this and also easily make us able to disable the graphics of our weapon without removing it from the scene we simply go ahead and create a separate camera to do this we right-click and go armed camera and I'm going to parent this to the normal camera so that it will follow everything that our normal camera does and make sure you reset the transform so that we don't have any kind of weird offset and what we'll do here is now add a lab layer separately for the weapons you can see I've actually already done this to do the same on your system all you need to do is select the weapon holder go layer add layer and you go ahead and type in weapons on the first one here then you go back to the weapon holder and you change the layer to weapons and we want to do that on the children as well so that our sniper rifle will also have the weapons layer assigned now inside of our camera and this is going to be our weapon camera we go and select calling mask here and we'd select nothing except for weapons so we only want this to draw the weapons and you can see that down here and we want to set the arm clear flags to depth only so you can see doesn't render anything like skybox so the color it only renders our gun and we want to set the depth here to 1 to make sure that it's on top of a the camera and again we want to adjust the near plane to point zero one then we select our main camera and in here we remove now on the weapon so under culling mask we disable the weapons and you can see now it doesn't draw that and we can actually set the near flags up again here so we can set them to something like point one a bit higher and now we should see inside the game that it's overlaid the web converting camera on top of the main camera and if we disable the weapon camera weapons are not going to be drawn so now we can go inside of Visual Studio we can add a reference to the weapon camera so we just go public game object weapon camera and then down here we go weapon camera dot said active to true when we're not scoped in and we want to set it to false when we are scoped in so now we should see that if we play here oops we also need to reference this of course and we also need to remove the audio listener the GUI layer and the flare layer on the camera here if we select a weapon holder we can now drag in our weapon camera and now we should see that if we right-click and scope in that our weapon indeed disappears when our overlay is enabled so that's awesome and we can go ahead and remove our cube now because if we play you will notice that that is indeed gone as well we cannot clip through that and this is a very very common trick used in pretty much any first-person shooter these days and some also do a bit of animation when you get close to the wall they normally use this technique on top of that as well so the last thing is just adjusting the fov on the camera if you don't know what if a we is it's basically field of view so the higher this number get the more we're going to be able to see from our surroundings and the smaller it gets the more zoomed in it's going to be in order to adjust this we simply need a reference to the camera here so we simply go in here create a public camera now because we'll be adjusting the camera directly and we'll just call this something like main camera and all we need to do is go down here and when we scope then we want to set main camera field of view and setting it to some smaller numbers say 15 or you could go up here and create a variable for that which we are going to do so we'll in public float called something like scoped fov and we'll set that to 15 up here and we'll also create a private float which is going to store the previous field of view that we had so that we can return back to the normal field of view so we call this either previous field of view or normal fov and we're just going to not default that to anything then down here we want to set normal fov equal to main camera dot field of view and then we want to set main camera dot field of view - scoped fov and then when we unscoped we simply set main camera feel the view back to normal f o V so that should basically be everything we needed to do oh actually of course we need to drag in our camera here so dragged in the main camera and now we can maximize the game hit play and you should see all of these neat elements working together we have animation we have zooming in we have a nice GUI overlay and what you can do of course is adjust to fill the view depending on how much you want this to zoom in and you can also adjust the speed of your controller and how fast you are able to look around to give an impression that the character is actually scoped in and might have difficulty moving and such and you can also add breathing effects all of that is up to you to play around with I just wanted to show you how to create this basic scope effect and we can enjoy the look of these awesome weapons remember if you want to get them you can just go to dev assets calm and if you choose to support that's freaking awesome on the site is powered only by you guys's support so that's pretty much it if you're making a first-person shooter I really suggest you check out this video on making a multiplayer FPS in unity it's a whole series and there's a lot of good stuff in there so I suggest you browse around also if you want to support these videos along with the different development projects such as dev assets you can always become a patron a patron comm slash Brad keys patreon helps you to donate a monthly amount of your choosing and you can cancel it at any time so it's really really awesome other than that I will see you in the next video thanks to all the awesome patreon supporters who donated in December and a special thanks to Sultan al-sharif Derek M jerk face Tamara Phi James Calhoun Robert Bonham and Jason Lotito if you want to become a patron yourself you can do so at patreon.com slash brackets
Info
Channel: Brackeys
Views: 265,938
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, model, models, beginner, easy, how, to, howto, learn, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, sniper, scope, zoom, modern, weapons, rifle, ads, aim, down, sights, weapon, fps, first, person, shooter
Id: adcKX1c-kag
Channel Id: undefined
Length: 26min 48sec (1608 seconds)
Published: Wed Jan 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.