How To Make A Third Person Sniper Rifle Scope | Aiming Down Sights - Unreal Engine 4 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to another management 4 tutorial in today's video i'm showing you how to create a third person sniper so this is going to be for a third person shooter i'm going to be creating a sniper rifle now this will also technically work in first person as well however it was designed for third but like i say it will still work in first so let me show you what we're gonna be making today we can walk in walk around i'll show you all the packs and everything i'm using to create this as well it's all free so we can walk around if we hold down right mouse button we're gonna aim in through the sniper rifle we have this on the screen we'll move about and we can still shoot like so now i've not added a sound effect just because i don't want to be too loud or distract or anything however you can see we have a sniper rifle like this and we can change how far we are zoomed in we still shoot and we just zoom out again like that out of the scope again we can change how zoomed in it is which i'll show you later on so this is what we can make today let me delete this code and i'll show you how i've done it so for this i'm going to be using three different things so i'm going to be using a sniper rifle model which i've downloaded the animation starter packs for the animations for aiming and stuff like that which i'd recommend you using if you're making a third person shoot or anything it's very good and it's free made by epic games and the third one is a sniper crosshair which i've made as well so i'll show you the rifle model and the overlay so this is the overlay that we've got obviously you saw at the start and then this is the model i've got it's not textured it's just one which i got from cg trader also you can use your own it doesn't matter we don't really need this for this to work however you will probably have your own one so once you've imported all that the first thing we want to do is want to set up the animations so i'm going to be using the character which i got in the animation start pack so for me i'm going to go to animstarter pack and you see we have this all here so once we're in here i'm going to create an animation blend space now the reason i'm doing this is because i want to have the animations for walking forwards backwards and all that while i'm aiming however if you don't want that you don't need to do it or you can use upper body montages anything like that but as well especially if you're using the adam starter pack i'd recommend doing this but doing it anyway so you can blend between the animations so to do this we're going to right click go to animation get a blend space setting it to the correct skeleton for you so mine's uefa mannequin skeleton with the path of game and in starter pack so it's the correct skeleton now select that and i'm just going to name this one sniper ads bs for playing space like so in ads aimed out sites and we're going to open it up straight away in here i'm going to change the horizontal axis to be direction when the minimum is minus 180 and the maximum at 180. then we can change the vertical to be speed the minimum is zero maximum as your maximum speed so i'll leave it as default for hundreds but change that to be the maximum speed that you have then in here i'm going to search for rifle iron sights as that's what the animations are named for me in this pack so i want the walk forward rifle iron sights and i'll put that all the way over from the right here in the middle and then walk backwards right flying sights all over there so i can walk forwards and backwards in here like so and then in the middle here as well we're just going to get a rifle iron sight idle so there they'll be in the middle at the bottom as well so you can see we're idle there walking forwards walking backwards and this is just the very basic blend space that we've got set up so once you've done that we can save and close that then we're going to put this into our animation blueprint so for me that's here ue4 asp hero tpp animation blueprint here and what i'm going to do is go to locomotion so this is our state machine so you might have to double click the state machine there i'm going to right click add a state and just name this sniper ads like so this will go into the idle and the id will go into this you can connect these lines into anywhere you want to go from so if you're jogging you can go into it as well and so actually i will connect it to jog as well to show you how to do it it's very easy to do so we're going to double click sniper ads and in here we will just place our blend space that we have that we just made to my sniper ads bs there plugging the end result into the result there direction as our direction and speed variables we should already have now if you don't already have these i do have a video on setting it up however if you especially if you're using this you probably have it already so we compile and save that now you can see this isn't working and that's because we haven't set up these transitional lines here but first we need to actually create a boolean so first we'll go to the event graph as this is where we're going to set it up but we need to do something first we're going to minimize this and open up our character blueprint which again for me is going to be this one here once you're in your character blueprint you want to find some empty space and we're going to right-click and we're going to get a right mouse button actually i'll set up an action mapping so what i'm going to do is go to edit project settings once this loads we're going to go down to input down here here plus action mapping there and i'm just going to name this one aim or ads anything like that i'm going to change this to be the right mouse button like so so we have to hold the right mouse button to aim or toggle any way you like we just have to use the right mouse button and the benefit of doing this is it's much more efficient than just getting keyboard event as we get in actual action mapping and we can set this up to have multiple keys set up for different consoles and also key bindings once we've got that we're going to close it there go back an event graph right click and search for we just named it so mine's aim so we should have action event aim there out of this want to hit the plus variable again to create a new variable this one named is aiming question mark leaving it as a boolean and setting it to true off of pressed and false off of released when we are holding right mouse button we're going to be aiming now if you want to have it so it's toggled all you do is you kind of pressed okay flip flop a sets it to true b sets it to false and don't come out of released and that will then toggle it however i don't want to do that i want to hold it instead so i'm going to leave it like that then we're going to compile that and go back to our animation blueprint here now you can see we have all of this if you're not using this animation blueprint in this pack don't worry you'll just come out with your cast which again i have a video on setting up an animation blueprint if you don't have one already so come out as your character i'm going to get is aiming out of is aiming we're going to right click promote to variable name this one is aiming as well and this is just so that we can use this boolean in this animation blueprint plug that to there so it will be set like so compile go back to our state machine and we're going to set this up so we'll do idle to sniper ids first so double click that we'll get is aiming and just plug that in there so when this boolean is true so we are aiming we're going to aim and then we'll go back to our state machine and go to sniper ids to idle get is aiming again this time we're going to drag out and get a not boolean so when we are not aiming we'll go back to the other one so go back to being idle go back to the state machine and we can do the same here so jog to sniper ads we just want to have as is aiming plug straight into there because that will be true or false which will set that to true or false and then sniper ids back to jog is not aiming so is aiming not building because if that's true that means we're not aiming so we'll enter the animation and play the correct ones now if we compile save we have no issues we can close the animation blueprint leaving the character blueprint open minimize this and we can actually hit play to test this out to see if it's working so we hit play what we're going to get is if we hold right mouse button we're going to aim like so and we can walk around like this and this works great now we're getting a bit of a weird thing there the animation doesn't look too good so i think i may use the wrong animation possibly i'll have a look in a second but you can see we are aiming when we're holding right mouse button like so so let me have a look at that animation and i'll get back to you okay so i have fixed it all i did was just increase the speed it needed and that worked i'm not sure why it did that this time because when i tested out earlier it didn't do that hopefully you can see this works i'll show you in a second however you can see we have our aiming done here so now let's move on to the next part but again what i did was this was in the middle i just moved them both up to be on the top line and now it worked perfectly so we can save and close that again next we want to set up our sniper rifle blueprint so we're going to minimize this go to content and we'll go in our folder which we want so mine's sniper scope here i'm going to right click go to blueprint class get an actor i'm going to name this one sniper bp i'm going to open it up straight away in here i'm going to add component add a static mesh with this one being our orp so i'll just name it or and i'm going to change this to be our static mesh sniper rifle that we have here now you can see by default this is quite big and not where we want it positioned so i'll use the values which i got earlier to fix this to recenter and resize it so the location was zero zero minus 40 on the z rotation was zero zero zero and the scale was point one five on the x y and z and now we have a properly scaled and centered gun and compiled that once we've done that we're going to add in the camera so we're going to add a component add a camera like so and again i'll use the values which i got earlier which was minus 1.15 on the x 0 on the y and 9 on the z with a scale of 0.25 on the xyz and now we have it positioned where we'd like at the end of our scope like so of course you can move this to be wherever you like however that's where i want it and the scale doesn't matter either it's just a lot easier to see obviously if we scale it down like so and then one final component want to add in here is again add component i'm going to add a sphere like so i'm going to name this one bullet and this is where we want the bullet to spawn when we're shooting from the gun so this is just a reference so what i'm going to do is i'm just going to move this to where i want again using the values i found earlier so that was 0.1 scaled on the x y and z and then about 72.5 on the x and then also 1.15 on the z just to raise it up a bit and you can see that we now have that at the end of our barrel there so this is where the bullet will spawn again put this wherever you like however this is where i found to be good for this specific model and now once we've done that that's the main part of the gun setup so it's the viewport setup we need to come back to do some code later on but we do have that part set up so we compile save that and now we're going to set up the projectile so our bullet so we're going to minimize this right click get a blueprint class and get another actor this one i'm just going to name sniper bullets projectile anything you want and then open it up straight away in here i'm going to add component add a sphere collision not a sphere sorry a sphere collision there and we're going to drag that onto the default scene root to make this the new scene root then we're going to add another component and add just a normal sphere now like so and this one i'm just going to scale down so it fits inside this three collision like so so i think that's going to be good we compile that and select our sphere version again we need to change its collision settings so we're going to scroll down until we find the collision we'll change collision presets to custom and change it from object type while dynamic to physics body now you may find other collisions which work a lot better for your projectile because at the moment i'm not actually setting up to be a proper projectile which damages or anything like that this is just kind of a placeholder for me so i can actually use it to shoot so you might have better settings for your own projectile if so use those that's absolutely fine but that is that setup and again if you already have your projectile you can use that so in compile save and now we can close the bullet as that is that part done next we're going to go back to our character blueprint here what we're going to do is we're going to come under the aim shall go above the aim i'll right click get event begin play if you've already used it you can hold on s left click to get a sequence after it putting your code in then 0 and then this new code in then 1. however i don't need to do that off of event begin play i'm going to get a spawn actor from class with the class being our gun which we just made so i named mine sniper bp there the spawn transform we're going to right click promote a variable and name this one sniper transform and we'll set that up in a second and put that down there out of the return value we're going to right click promote a variable and call this sniper reference or sniper ref out of that we're going to set actor enable collision and we're going to untick it so it's false so we want to disable the collision so that it doesn't interfere with the player and start moving it in random directions and breaking the code so also make sure that the sniper f is the target for that enable collision out of the sniper graph again we're going to attach actor to component plugging that in there with the target as the sniper f and the parent as our mesh so get meshed there as we want to attach the sniper to the mesh so it moves around with us the socket name for this i want to be the right hand so i'm just going to quickly get something so a cube dragging it onto the mesh there searching in the parent socket for the right hand so you see is all lowercase hand underscore r so i deleted that cube it was just a reference and in the socket name i'm going to put hand underscore r now it might be different for you if you're using a different skeleton so make sure you get it spelt absolutely perfect otherwise it won't work and for the rules we're going to keep it as keep relative for all of them after this we're going to create widget like so and we'll create the widget in a second return value of this i'll actually do that in a second as well so i'll delete the widget we'll do that in a minute what we'll do first is we'll set up our sniper transform so if we go to the viewport add a component add a child actor here i'm gonna set the child actor class to be our sniper bp here now this is just going to be reference we'll delete it in a minute we're just using this to get the proper location we want so once we've done that we'll also parent it to the mesh as well so using that hand r there and this is where it would be so now we can rotate it and locate it where we want it to be so this might take a bit of fiddling for you obviously you can use the values which i'm about to create as well and what might be better as well is if you actually change the animation blueprint to be an asset of the rifle iron sights so you can see where we positioned when we're aiming again do it for how you want so i think that is a good position for me that's where i want the gun to be so what i'm going to do i'm going to again select the mesh change it to the animation blueprint now you can see we have the gun perfect there like so what we'll do is we'll select the child actor again so we can have its transform variables here and select the sniper transform so we can get the two there with the child actor selected we're going to right click on the location hit copy go to sniper transform right click location paste do the same for the rotation so right click copy right click paste so we're just using this as a reference to get it where we want it to be and the scale should be the same as one and now we can delete that child actor as we don't actually need it so that's perfect so i'll go back to the event graph here you can see the sniper is going to spawn where we want it to so now let's set up the widget so we're going to minimize this right click go to user interface widget blueprint and i'm just going to name this sniper overlay widget like so or sniper across there anything you like and in here we're going to add an image with the position of 0 on the x and y size x 1920 size y 1080 so let me just fix that there we go and we'll anchor it to the whole screen as well so it doesn't move about then under appearance we'll change the brush and the image to be our sniper overlay which we've imported like so and this is what it's going to look like so compile save and that is what it will look like when we're aiming down the scope so we can close that as that's all we need to do in there back in our character blueprint i'm going to come up with the attach actor to component again create widget like we were going to earlier with the class as our sniper overlay widget there the return value for this we're going to right click promote to variable i'm just going to name this overlay ref you name this absolutely what if you like it's just a reference to that overlay widget we've just made and so now let's set up actually aiming so we set up the aiming animation spawning in the gun all of that now we need to actually set it up so that we can visually look down the scope when we're aiming so we're going to come off of our aim code we made earlier down here so first we're going to right click down here and get player controller out of this we're going to set view target with blend plugging that into set is aiming false so we'll do that one first the new view target is just going to be a reference to self so when we stop aiming we want to look through the character's normal camera again like so the blend time i'm going to put is 0.2 this is how long it's going to take for the cameras to blend between each other so how long it takes to go from the player's camera to the sniper camera now if you want this to just be an instant snap leave it at zero i wanted to just quickly blend in and out so i'm gonna put it as point two so what we're gonna do is we're gonna get the overlay ref so get overlay ref there and out of this we're going to remove from parent so it's gonna remove that widget and the crosshair from our screens like so then what we're going to do is we're going to select set view target with blend control c control v to duplicate it up here plugging that into set is aiming true player controller will again go into the target this time the new view target is going to be our sniper reference so we can get sniper f there so now we're going to be looking through the sniper's camera when we start aiming so that should work great for us and then i'm going to come up the overlay ref again and add to viewport so this time it's going to add this to our screen when we start aiming so what it's going to do is when we start aiming we look down adds it to our screen and we stop it will go back to our normal camera so this will work however there is more stuff we need to add after this however like i say this will work for the moment so let me just show you what this is actually doing so we get in we're going to aim down there so you can see we aim down however we can only look left and right not up and down and we still have the barrel there now if you want the barrel to be there that's absolutely fine you can leave it there and you can also see we see the bullet reference that we left there earlier and that's just because we forgot to change one thing so i'll do that now but again if you want the barrel in there leave it as that but there is still more stuff to do after that so we're going to come off this open up our sniper bp here we'll select the bullet i'm just going to take hidden game compile and now we shouldn't see that in there anymore and that works a lot better like that so now let's finish off this code so go back to our character blueprint here what we're going to do is we're going to get the sniper reference here get sniper ref out of this we're going to get the awk or just basically our static mesh for our gun then out of that we're going to set owner no c plugging that into add some viewport there and we're going to tick it so the owner basically the player doesn't see the mesh when we're adsing so other people will still see it but not the player which is perfect so we don't see the barrel down the scope and we'll duplicate that down here plugging in to remove from parent target again the orp or basically your gun we're going to untick new owner oc so that we then do see it and then after this again we're going to get the sniper reference and out of this we're going to enable input leaving it as the target i'm going to come out this again and disable input again being the target plugging that in there player controller will just be get player controller for both of these like so so plug that there like that and the reason we're doing that is so that we can move the camera when we need to when we're in that blueprint so we compile and save that and we've only got one final thing to set up which is moving the camera so that we can then look up and down as well so that's very simple so we're going to do is going to our sniper blueprint here and go to the event graph and what we'll do is we'll delete these three events here instead we're going to right click and we're going to get the look up so axis event look up there what we're going to do is we're going to hit the plus variable here name this one pitch and i'm going to change this to be a float and drag and drop get pitch and then get an addition so i float plus a float putting the bottom value as our axis value from the input axis look up here out of this we're going to get a clamp float there with the minimum and maximum values as how far we want to be able to look up and down so i'm going to set the minimum to be minus 60 and the maximum to be 60 so we can only look up and down by that angle so i'll show you how i found these values what i did was i just dragged in our sniper blueprint here you can see in the bottom right what it's going to look like if we go back into our sniper bp and we just go into the viewport minimize it a bit like this so we can see it and we just rotate our camera on the pitch you can see we're looking up and down so i want to be able to look that far up and that far down so that was 60 and minus 60. so again get these values to be perfect for you and that's how you do that so again mine are 60 and minus 60 like so and out of this i'm going to set the pitch again so just make pitch there i'm going to plug this into the input axis look up like so so this just means that we can then continually add on to it when and where we need to clamping it between these values out of this i'm going to get a float multiplied by a float times it by minus one because by default this is actually inverted so times get by minus one sorry not minus ten times thinking by minus one just reinverts it back to how we normally have it so when you move your mouse up it goes up so if you want it to be inverted don't have this then i'm gonna move that down i'm gonna drag a reference to our camera in here i'm going to set relative rotation plugging that into the set pitch there now we could make a rotator here but what i'm going to do instead is just right click the new rotation split structure pin plug in this return value of the multiplication here into the new rotation y for the pitch as that is what we want to be changing as now this will allow us to move the camera up and down as well and we can only do this when we are aiming which is why we enabled and disabled the input over here so this should now work perfectly for us as well so actually there's one final thing and that's just allowing me to be able to shoot the bullets so we compile and save that and close our sniper back in our character blueprint we'll find some empty space we'll right click get a left mouse button now again you can get the action mapping like we did earlier up here however this is just temporary code for me so i'm going to do it like this but again i'd recommend making an action mapping out of pressed we're going to spawn actor from class with the class being our sniper bullet that we made earlier spawn transform we're going to make transform like that you can put the scale as what you want but i want to have it as 0.5 as when i was making this code earlier i found that half the actual size of the bullet was good for me but you can set this to be whatever you like for the location which is where we want to spawn it we're going to use the bullet reference we made earlier so we're going to get sniper reference there out of this we're going to get bullets and you might have to scroll down get bullet there and out of this we're going to get world location so wherever the bullet is at that moment in time is where we want to spawn it and the rotation that's very simple as well we're going to get the sniper reference once again down here out of this we're going to get camera scroll down get camera or what have you named your camera and out of that we're going to get world rotation plugging that into the rotation of the mate transform and that is it done that is how we're going to spawn the bullet and where we want to spawn it as well and that's all we need to do for it so if we compile save minimize this should be working for us so i'll delete that and we can hit play to test it you see that i left click there to get in and it's spawning the bullets however they're not moving so i will actually finish that now it's very simple we need to open the sniper ball again and we need to add a component so we didn't actually add the movement for it so that was my bird i forgot about that so we'll add component add projectile movement and then we're going to search for velocity and we're going to change the velocity on the x to be about 3000. so we can close that compile and now this should work for it so we hit play left click you can see we're spawning in bullets there like so so if we hold down right click we're going to go in so we're adsing so you can see when we hold right click to aim we are actually aiming in and out of the sniper like so with the correct vision on screen and the correct camera and we can move our camera up and down we can't move it any further up or down than what we decided we can move it around like so and still shoot wherever we want to like this as using the camera's rotation so that works great so i also remember i'd say how to zoom in and out so that's very simple i'm going to come out of this go into our sniper bp here we'll select the camera and we can just change the f of v so the lower the f of v the more zoomed in it is the higher the more zoomed out so if i set this to 15 this should be more zoomed in now so i hit play right click you can see we've really zoomed in now so now if i look over there you can see we're much more zoomed in than what we were before so that's how that would work so i think that'll be for this video we've done everything we want to do we've set it up so we can aim in and out down sights through a sniper let me actually just zoom out a bit again so that's quite far zoomed in for this map so we can aim in and out down sights like this from third person to first person for our sniper rifle like so we can change how zoomed in it is and we have this widget on screen as well we're gonna move the camera up down left right and we can still move around and this works perfectly so thanks so much for watching i hope you enjoyed and i hope you found it helpful and if you did mr like subscribe down below so thanks so much for watching and i'll see you in the next one
Info
Channel: Matt Aspland
Views: 11,566
Rating: undefined out of 5
Keywords: ue4, unreal engine 4, unreal engine, tutorial, ue4 tutorial, unreal engine 4 tutorial, how to make a game, how to, games design, ue5, unreal engine 5, tps, fps, shooter, first person, third person, first, to, third, person, sniper, scope, zoom, 4x, 8x, 15x, overlay, classic, cod, bo4, widget, image, crosshair, cross-hair, cross hair, cross, hair, in, out, camera, cameras, change, between, ads, aim, aiming, down, sight, sights, ue4 ads, ue4 aim, ue4 sniper, ue4 sniper scops, ue4 tps, ue4 fps, third person shooter
Id: BdE0f9g9akw
Channel Id: undefined
Length: 26min 22sec (1582 seconds)
Published: Thu Feb 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.