Procedural Recoil System | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my fellow gilberts and gilberettes and welcome to an fps tutorial regarding a very important topic recoil recoil is extremely important and is one of the vital trunks for gunplay in the fps genre i've been studying a multitude of games to find out how recoil systems work and to see if i can replicate something similar [Music] i wanted to study the games i find feel the most satisfying when it comes to recoil titanfall and apex legends were at the top of my list [Music] modern warfare was a close second as well but generally this ricoh will feel very familiar to most fps games there's probably a much easier way of achieving this or a much better recoil system but with my research i haven't really found that much this tutorial isn't going to be too beginner friendly so hopefully you have some okay experience with unity be prepared to do some coding and this tutorial will also require some setups that your fps project should already have [Music] first things first you're going to need a proper character controller some player controllers i've seen have the camera as a child to the player while the player rotates its orientation with the mouse and then the camera's x rotation is controlled separately my player controller controls the camera completely by the mouse and does not rotate the character i don't think this really matters but just throwing some context in there make sure you also have a weapon script ready to go that you can call a shoot function when clicking if you don't have these there's a lot of tutorials on youtube [Music] for our setup i'm going to create a game object attached to my player that is in the location that i want my camera so this is going to be pretty well where the head should go this is the object that will rotate with the mouse instead of the actual camera game object i'm going to name it camera rotation then create another game object underneath called camera recoil this object is in the same position and rotation as our camera rotation object and will carry our camera recoil script stack your main camera and weapon camera underneath if you just use one camera that's fine as well just put the one camera underneath i personally attach my weapon game object to the weapon camera as well [Music] we are now all set up and ready to start coding i'm going to create a new script called recoil and attach it to the camera recoil game object let's open that script and get started first thing i'm going to do is remove the stuff we don't need like system.collections and we also don't need these starting notes here so let's start off with some rotations we're going to create two private vector threes one we will call current rotation and then the other target rotation now we're gonna work with a few values so i'm gonna make a section for hip fire recoil i'm also going to make these values private however we do need to change these values in the inspector so let's use serialize field and then i'm going to make three floats recoil x recoil y and recoil z and then we will make a little section for our settings let's make a float for snappiness and then one for our return speed and i think that's all we need for now let's just go to our update section and start on the main part of the recoil first we're going to assign target rotation so target rotation equals vector3.lerp and we are going to lurp it to vector3.0 so let's put target rotation vector3.0 and we're going to use return speed for this and of course we're going to multiply it by time dot delta time just so everything works well and then we are going to work on our current rotation so current rotation equals vector 3 dot slurp and we're just going to use slurp instead of lerp because slurp is really good for rotations or directions i guess and it seems to just work better than lerp in this situation and then we are going to move it towards our target rotation so we're going to go current rotation target rotation and then we're going to multiply that by our snappiness so basically the speed and times that by time dot fixed delta time and end that off right there and then we are going to actually start the rotation so we're going to say transform dot local rotation equals quaternion euler current rotation and that's perfect now if we just go back into unity it's not going to do anything if we shoot so let's make a recoil fire method so i'm going to make a new method called recoil fire and this needs to be public because this is going to be called from our gun script so let's start by saying target rotation plus equals new vector 3 and it needs to be a new one because it's just going to be a new one every time and this is where we use our recoil values so we're going to use recoil x and then we are going to go random.range negative recoil y and recoil y we're using random.range because we want it to be procedural and what it's going to do is it's going to take your value that you put for y and it's just going to go in between them we didn't do that for x because x is our up and down and we don't really want that to be random i mean if you want to be random that's up to you but i think most games don't do that and then we're going to do the same thing for our z-axis and end that off right there now our final step to just make sure that this is working is to get this set up in our gun script so let's hop on over to our gun script apologies as mine is a little bit messy right now i'm definitely working on it we're gonna need to reference our recoil script so if i go up to the top here i'm going to put private recoil and then i'm just going to put recoil underscore script and then in my start method or awake method whatever you want to assign it to we're going to put recoil underscore script equals transform dot find camera rot slash camera recoil dot get component recoil and that should assign it that should be fine and then we are going to come down to our shoot script and put recoil or sorry uh recoil underscore script dot recoil fire and then that way every time the shoot function is called it is going to call our recoil fire method this should be placed alongside your shooting animation which will then lift up the target rotation and kind of turn it and it will just keep going every single time but remember in our update function our object is trying to go back to the center at all times so this will kind of make it springy and every time we shoot it just moves up and then comes back down [Music] and if we go see this in unity this should work perfectly let's just make sure to change some values in the inspector so you can play around with this and change it to whatever you want obviously it depends on whatever gun you have for just a regular rifle recoil i like to do negative 2 for our x 2 for our y and 0.35 for z and then for snappiness we can go 6 and return speed we'll put down to two and as you can see it looks pretty good [Music] and when i stop shooting it just goes back to the center now there's a few things that we can improve on this first is when i'm aiming my recoil should be a lot better the second is that we should have this tied to the gun and then that way we can change the settings per gun and it'll just work based off of that so let's start off with the aiming recoil this should be pretty easy um as long as you have aiming setup on your player movement or on your weapon script so let's just copy and paste our hip fire recoil and we're going to call this ads recoil and instead of recoil x it's going to be aim recoil x and the same thing with y and z and then we need to see if we are aiming or not so for me my player script is able to check if i'm aiming or not so at the very top of my recoil script i'm going to say serialize field private player player script and obviously your player might be like player movement or uh if your aiming is tied to your gun it might be gun just get that set up there and then we are also going to create a bool and it can also be private and i am just going to put it as is aiming and then we can go down into our update method and assign that so let's say is aiming equals player underscore script dot aiming whatever your aiming bool is in your other script there's definitely other ways of doing this but i feel like this just looks the cleanest for my code and then our next step is we're going to go down to our recoil fire method and i'm just going to cut what we have in there and then i'm going to make an if statement so if we are aiming if is aiming um let me paste this again and then we're going to change all of our recoil x y z values to aim recoil x recoil y and recoil z and then else and we're just going to paste the original line of code and that's perfect so that should work let's go back into unity and first things first is we have to assign what our player is so i'm just going to drag my player into this slot and then we are going to also change the values for our aim recoil i like to use negative 1.5 1 and 0.3 and there you have it when we aim it looks like our recoil is quite a bit tighter a little bit easier to control now our next problem is we only really have this one setting and that's gonna go across all of our guns but i want it to be so that every gun has its own settings so this should be pretty easy all you really have to do is copy all of these float settings to your gun make sure that they are public and then we're going to reference our weapon script or loadout script on our recoil script and then in our recoil fire method instead of recoil x and recoil aim x and all those we are going to go loadout script dot current gun dot recoil x and then we're gonna do the same thing for the rest of them obviously this is really gonna vary um depending on what scripts you have and how your whole weapon system works so i'm not really going to go too much into detail with it because if i show one way a lot of people will be pretty confused if their system works quite a bit different than mine now i just want to say that this recoil system isn't perfect but it does seem to be working pretty good if you're having any troubles with this tutorial please let me know you can leave a comment on this video or you can even join my discord server and ask for help there this isn't going to be my first recoil tutorial as it's mainly just covering the camera currently i'm working on a demo for this game and if you're interested be sure to also join the discord server and you can become part of the play testers that is everything from me today definitely let me know if you enjoyed this tutorial and if you want more i definitely want to go a lot more in depth with things i feel like this was a pretty quick tutorial and if you're a beginner it might be a little bit difficult to follow so i definitely apologize for that alright everybody have a wonderful day and see you in the next video
Info
Channel: Gilbert
Views: 10,920
Rating: undefined out of 5
Keywords: Unity, Tutorial, Unreal Engine, Gilbert, Recoil, FPS, First Person Shooter, Video Games, game Dev, procedural
Id: geieixA4Mqc
Channel Id: undefined
Length: 15min 38sec (938 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.