[#09] Unity Animation Rigging: Weapon Recoil Patterns using Overide Transform Constraint

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Kaldur welcome back on the Kiwi coder and this video I will show you how to add weapon recoil to your characters and your weapons so the recoil system has got both muzzle climb and these repeating recoil patterns which can be counterbalanced by pulling the mouse cursor down and there's also camera shake and there's within recall animations you can see the the shoulder on the character sort of kicking back here and both the the pistol and the assault rifle they're created using the exact same system and they've got slightly different animations but the rest of it is just different parameters into the system okay cool so let's get into it for this video follows on from a previous video I made the switching weapons using the multi parent constraint so in the scene here i've got a character and when i shoot there's there's no recoil animations happening so I want to make it to the crosshair basically it drifts up the center of the screen as I hold the of the fly button down and to do that I'm gonna use some cinema sheen to modify these these values these access control values that have been fed into cinema Sheen the cinema sheen free look component that exists on the the character so this y axis there will be our vertical recoil and the x axis will be the horizontal recoil and we'll just modify these values and encode so to do that just start by getting a reference to the cine machine free-look camera so inside the active weapon script we will get create new property called sue machine free look I'm just gonna call it player camera and can assign that in the inspector just like that and now we want to basically assign this to the weapons runtime so just create a new script called within recoil on the weapon prefab I'm just gonna use the assault to start with and this also needs a reference to the camera except this one will be assigned dynamically so I'm just gonna use the slide inspector attribute just to make it clear that it shouldn't be assigned in the editor and also the raycast within the script needs the reference to the weapon recoil script on the same game objects just cuz we'll call them to that each time we fire a bullet so just use get components because it's on the same game object inside active with them when we equip the weapon we just want to do the same thing that we're doing with the crosshair target and just do like weapon recoil to play a camera Nicolas play camera cool so yeah now we've got the within recoil scripts hooked up and this has been assigned as we equipped with them you can create a new function will generate recoil and this will be modifying the access position of the camera and so we just need to call that inside like fire bullets from the weapon so each time we fire a bullet we just want to call generate recoil and then finally we just need like a new public float property called like vertical recoil and this will be how much to actually modify their access by so you can just do play camera y-axis dot value - Eagles for going up the screen vertical recoil and the last thing is actually just assign a vertical recoil amount inside the that our prefab here so if I just say that to one I'll show you how that looks cool so yeah the the recoil amount is still whole like that's huge so if I just modify that just reduce it slightly to like 0.01 yeah it looks better except it's still quite stuttering like pretty much yeah just want to like smooth it over multiple frames so rather than calling rather modifying the axis value each time we generate a bullet I'm just gonna start a timer and then modify it over multiple frames instead so just create a new property called duration and this will be like how long each each Greek oil sort of hit will take and then create another property called time and we just set that duration just reset the timer to the duration each time we fire a bullet and then inside updates just check if there is time left then subtract the vertical recoil amount and then we just yeah subtracts time got Delta time from the timer and the last thing because we're doing this over multiple frames we now need to just multiply that by Delta time cool so this I need to just set a duration now so I just said to like one second yeah you can see it takes now like a second to uh to travel up the screen but there is still like an annoying problem like if I say this to like 0.1 for example notice the distance it travels it's like traveling basically just over a seguir and if I change the duration now to like 0.1 if I wanted to go much faster see how it doesn't travel the same distance anymore it makes it really hard to tune because now if I change the duration I have to counterbalance the vertical recoil which is annoying so to fix that we can just divide this amount here by the the duration and it's not going to be exact because there might be some left over time and stuff like that but it will make it much easier to tune so now you can see it's traveling about just over a square and if I change the duration to 1 the travel was pretty much the same amount which just makes it like so much easier to tune now I can just reduce this vertical recall command to late so appliance or one for example and reduce the duration down to light 0.1 yeah and now that's looking it's looking much smoother the last thing is actually um I just don't quite like working in values like this is 0.01 it's a little bit awkward to type so rather than doing that there I'm just going to divide this vertical recoil amount by say like a thousand and that way I can use sort of more integer kind of units it's the minor thing yeah it just means if I if I type 1 it's now moving like a really small amount I type 10 yeah cool so the last thing is when I stop firing the cursor just it just stops straightaway so I actually want to have some kind of I guess recoil where the cursor kind of goes up and then comes back down and to do them can we use the semi machine impulsive extension but first just make sure to set any properties that you changed in the scene if you were changing it like I was on to the actual prefab itself cool so now if you go to sorry a city machine free local camera and then scroll down to add extension and just add a cinema sheen impulse listener this listener thing will just allow a new machine to receive impulse events and impulsive ends are generated from a cinema sheen impulse source component so just add one of them to the weapon prefab and it needs a signal shape so you can create one in the asset menu under cinema sheen the noise settings and lastly just to sign their asset to send a machine m4 sauce so we need to actually generate impulsive ants from the RICO script every time we fire a fire a bullet so we just need to get a reference to this this component here inside weapon recoil just create a new property hide an inspector again because we'll get inside awake and send a machine to machine and call source camera shake like that and just inside awake you can just get camera shake the usual way pull source and every time we generate recoil we also just want to call camera shake tor to generate impulse and this needs a velocity so it should be relative to the camera in our case surface use camera domain dot transformed forward cool look at the asset there's a ton of properties on here you can basically change the rotation or the position of the camera all of it is just set to zero right now so nothing happens see our first shoot basically it's just doing exactly what it was before but now if I am just for example if I change the Z rotation of the cameras or something crazy like 90 you can see the camera light twists and if I change the frequency to like 10 they no actually twist both ways make you feel like super drunk so I'm just gonna clear that one the one I actually want to use is rotation X so if I hit like minus 20 you can see it's now rotating about the x axis of the character and the across it actually pulls back down as well quite nicely yeah looks pretty cool the other one that you can change is like the the position of the camera I'll show you what that looks like so if I did 20 you can make it like zoom out maybe you want to have it like minus 1 or something you know so when you're shooting it the camera light pulls in a bit yeah you can play with this stuff all day long because yeah that's basically the basic idea so you can change the the camera settings for your recoil you there's no settings and then change the actual crosshair recoil using using these properties here on the weapon recoil script so it's quite a nice distinction between how the crosshair sort of moves and how the camera is affected rather than trying to mash it all together I think it is quite good to separate these concepts out cool so the next step is just going to be adding like some horizontal recoil and just giving those those light recoil patterns in so you can set that up pretty much in the same way that we did the vertical recoil so just create a new property here called horizontal recoil and then ya rather than modifying the y-axis we'll just modify the x-axis here and use the horizontal recoil parameter here so the units are a little bit different for horizontal you only need to divide by 10 to make them in roughly the same range so now just on the prefab itself just set up like a horizontal value something's gonna say that the same as the vertical 10 and it should go up and roughly 45 degrees to the left which ya so that's cool you kind of want to say so you go back and forth so you could you know randomly generate a value between positive and negative of this but instead I'm gonna use a vector to a rake or recoil pattern and then easier to explain if I show you what I'm talking about so rather than having just one vertical recoil sample I'm gonna create like multiple of these and then basically specify the horizontal and recoil values for each each time we fire a bullet and then basically index this array and increase the index each time we fire a bullet so to create like a test pattern you know you would start going to the left and a little bit less then a little bit less then you know less some more and let me just finish filling this out really quickly and for the y-value I'll just keep that the same I'll way down so now just inside the weapon recoil script we just want to sample this array so yeah actually one thing I forgot to mention is the vertical recoil horizontal these are now redundant so they can they can be made basically private here and we'll just initialize these values every time we we generate a recoil sample this this index here lissa right here using our index so the horizontal recoil would be the recall pattern at their index with the x-value and then for the vertical recoil use the y-value and now we just need to increase the index but I want it to wrap around when it gets to the end of the Ray the end of the array so let's create a new function called like get will like next index index and then you could some you could just randomly sample this array as well that would be quite could make it quite interesting but in my case I'm just gonna go like index plus 1 and then wrap it around is in mod recoil patterns of links go and then here we just do index equals next index passing in the current index and yeah none of those code needs to change because we now just initialize these values out of this array and yeah so with any luck this should look like I can guess pattern nice and uh yeah it's pretty cool the one thing is I actually want to reset the where the index sort of begins from every time I start firing so I'm just gonna add a new function in here like public void reset and then just hit the index equals to zero and inside recast within and so start firing just cool like recoil reset that just means we always start from the beginning of that that pattern every time we we start shooting you could also do things like decay the index over time when you um when you stop shooting yeah there's tons of different ways you could you could do this cool so I think the final thing let me just check all this is still working the final thing is now just adding like some animations to the character so this one I'd like some actual animated like recoil on the character to move the weapon back and forth so I'm gonna create some new constraints actually the first thing I'm gonna do is animate the body of the character so I'm gonna call this body recoil and to do that I'm going to use a new component called the override transform component and for the constraint objects just assigned spine to and then assign this body recoil as the source object so what's it what this lets us do is we see add on some transform values to like some position and rotation to this spine node basically I've modifies the the pivot position of this bone here so if I go into plane mode I can kind of show you what I'm talking about yeah so if I modify the Y position of this body recoil and now it modifies the the spine position of the character which is pretty cool and it's all relative to what the character is doing so if I look at all the aim stuff is still working and now this has been applied and afterwards and if I zero zero everything out then it's just it does everything kind of like as though it had no effect except there is one kind of issue like if I turn the character right around it's now like looking at me and I want the head to just always be looking forward so this aim here constraint this should come after the body recoil and now if I go into play mode and modify the Y position the head actually stays in place if you go to the extremes if it looks pretty cool but yeah just don't do that and the last thing is actually just a modifying the Wiccan holder position so currently this holder position is a she it's being keyed in somewhere here the weapon on the multi position constraint as being keyed to the what's been bound to the clavicle are joint so just create a new constraint afterwards called like weapon recoil and again I'll use the override transform to modify the holder position using this in recoil object and all the values here will be relative to whatever is happening and the weapon holder game objects so let me just show you in the scene now I can modify this this recoil thing here and that lets me modify the pivot position and like if I aim this way up for example every all the aiming lair and stuff everything is still working as a as it was before but now there's just like a relative kind of animation that's played over the top which i think is pretty useful for recoil kind of stuff and yeah if you see row everything out then everything just works sort of as it was before so yeah now it's time to just a keyframe that stuff into the rig controller select the bricklayers game object and just open up the rig controller and we're going to create a new layer here called recoil layer and this will be responsible for playing our recall animations just make sure the weight is set to one and just need like an empty state basically which won't play any animation and we'll just return to the state after we finish playing our recoil animation and yeah so just actually need to create some animations so if I call this site actually yeah whippin underscore recoil underscore rifle and just drag that in so when I create a transition when the animation finishes playing so just sit have exit time to true and I'll just hit the durations to zero because I don't want any blending going back to this empty state and I'll create another animation for the pistol as well just cuz that will have a slightly different look so we want separate animations for that and again just at the exit time durations to zero cool so yeah I think that's that's it for the animator so if you just close that out then inside the we can go back to the code inside the weapon recoil scripts we now need a reference to the rig controller so just create a new property I'm just gonna copy and paste this animator and call this like rig controller and this will be assigned in the same way these properties were assigned from the active weapon scripts so that'll just be weapon recoil rig controller equals rig controller and yeah each time we generate a recoil we just now want to play one of those animation States directly so I think it was called weapon underscore recoil underscore and now we need the the name of the actual weapon so just pass that through from the raycast weapon script when we call generate recoil you can pass the weapon name through and just use that to create a string for the name of the with animation state and the second parameter is actually the layer which was layer 1 for the recoil layer and paths and 0 for the normalized time that way it'll start playing the animation from the from the beginning every time cool so now it's just time to actually start animating if I just grab a weapon to animate and go to the same view well and yeah it just goes to the rig layers and then open up the animation window and [Music] bottom here and just select the weapon recoil rifle so the first thing I'll do is just move the crosshair kind of like 20.3 cross the cursor whatever it's called and then hit record and I will animate the body recoil so probably just like a subtle effect I think it's better I'm not going to spend too much time in this video cuz I don't want to waste your time figuring out properties for these but this is just the general idea right you could also set this to like minus 4 to bring the gun up a little bit also bring it back a little bit something like that yeah and then just reset everything at the end of the animation I'll just leave it at that for now because you can toy around with this stuff in your own time but that gives a pretty kind of okay looking animation to start with it's quite slow at the moment so I'm just going to go back into the rig controller select the rifle animation and just speed it up I like six times and if we go back into play let's close this animation window now and open let's go out yeah you can now see the the gunner sort of recoiling up a little bit and the shoulder is sort of kicking back the screen shake is it's quite dramatic so it's a little bit hard to see but you can tune all that stuff in your own time but yeah this is how it looks in the scene view we've got a character and if I shoot up all of the animations are the relative to where the camera is looking cool so now the last step is just setting up the pistol which is gonna be set up a little bit differently so yeah just fix the pistol prefab we just need to add those two scripts that we added to the assault the assault prefab so that's weak and recoil and the impulse soon routine impulse sauce so for the impulse source just selects the noise settings that we're using for now you could create like custom my settings for the for the pistol there's going to zero out the sustain time as well I think it's kind of a good value to set to zero when you're working with recoil and then for the recoil pattern so I don't want to have that crazy is shape that I was using for the assault rifle so I'm just gonna have a single entry with zero zero and this just means that there's going to be no horizontal or vertical recoil on the crosshair position of the pistol so if I go into play mode we can show you what that looks like now when I shoot basically the the crosshair connect goes up and falls back down quickly and that is due to the cine machine and Paul sauce but the recoil let's all just zeroed out so it doesn't doesn't climb up the screen like you know in that crazy shape like we had for the assault rifle yeah which I think just makes a lot of sense for the pistol it's yeah Lucy yeah just how I want it and the last bit is just making it look cool so inside the animation for the pistol just selects the rig layers and yes lets the weapon recoil pistol obviously their cursor to 0.3 and inside the scene view she's gonna do something a little bit different first of all sit at the body position 210 just pretty similar to what we're doing for the assault and the the recall position itself I'm going to give it a kind of more unique look for the pistol where the gun goes up beside the character's head I think it looks quite cool and you know just sort of bring forward a little bit yeah something like that it's probably also worth just keyframing out to the left-hand head note so the elbow doesn't kind of flop around all over the show yeah I mean that looks it looks pretty cool and you know just again at the end of the animation it's just zero everything back out to make it go back to where it was cool so it looks awesome for like a 10 second animation and I think yeah this is one more thing if I just play that animation at normal speed it's quite slow so I just need to speed that up go into the rig controller select the recoil and I'll just set the speed to 3 yeah and lift I guess just checking her out on the scene so I grab a pistol really need some proper sound fix man yeah this looks really happy with this so the assault rifle has got this a recoil pattern which you can't like counterbalance by pulling the mouse down if you're any good at it and then the pistol that are theirs acts much more like a pistol which is pretty cool so yeah I mean that's the that's the end of this tutorial so hopefully you have learned something from it and yeah there's tons of different parameters to play with to get your weapons feeling the way that you want so just to kind of recap on the prefab you've got the weapon recoil script where you can control the recoil pattern plus the duration between each of these shots you've also got cine machine impulse source which you can use to control all of the camera shake that goes along with the with the recoil and then inside the rig controller underneath the rig layer you've got these animation States which are getting played and you can set whatever keyframes you like and so those those animations using the body recoil and the weapon recoil notes cool yeah so if if you enjoyed this video and just yeah like and subscribe and share it with your friends and all that and yeah I'll be back with more videos so stay tuned cocky
Info
Channel: TheKiwiCoder
Views: 14,577
Rating: undefined out of 5
Keywords: Weapon Recoil, weapon recoil unity, unity weapon recoil, recoil animation, override transform constraint, override transform unity, recoil animation unity, cinemachine camera shake, camera shake unity, Unity animation rigging, animation rigging, animation rigging unity, Unity, Unity3D, Unity tutorials, unity weapon, unity third person, third person character, unity character controller
Id: JD7iDleSzDA
Channel Id: undefined
Length: 27min 6sec (1626 seconds)
Published: Fri Jun 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.