[4] Inverse Kinematics - Attaching your Weapon Using IK || Multiplayer Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys it's your boy mike from big shot studios and we're back with episode 4. today we go into inverse kinematics but first i would like to thank all of our patreons that have donated to us as you can see here so this video is dedicated or not dedicated this video is is uh sponsored by jack isaacs matt parkins lincoln and daniel williams thank you for your generous uh donations we look forward to working with other people in the future if you would like to do the same you can go down to the description and click on the patreon link and it'll take you to where you need to go so let's go ahead and get started okay so the first thing that we want to do is we want to get into our scripts folder we're going to go ahead and create a new script and we're going to call it um ik i k controller wait for that to compile and we are going to go ahead and double click and open that up into visual studio once visual studio has opened up we can go ahead and like i do every other time is to go ahead and delete the commented lines for the boilerplates and we are going to do first thing is public static ik controller instance and then we're going to double uh double enter into avoid awake we're going to say instance is equal to this and that's all you have to do for that basically what this does is it makes the the script available no matter no matter where you are inside your project um and i'll be able to show you that later on down the road the next thing we're gonna need is under the uh public static ik controller instance is we're going to add animator and all right and then we're going to go into start and we're going to say anim is equal to git component animator and we're going to go ahead and leave it at that then we are going to go ahead and start filling out what we need first things first we're going to do the right hand um under the animator we're going to make a header and we're going to call this the right and ik and under the header we're going to say range and we're going to put it between 0 and 1 and then we're going to say public float right and wait and then we're also going to go ahead and get a public transform and this is going to be your right hand object we'll just call it obj and we're going to equal it to null under that one we're going to do a public transform right and hint and we're going to equal that to null as well and the reason why we do it to null is because we want to say if it if it's not there then it's not going to to throw any issues you know as far as errors go and then what we're going to do is in i don't believe we need update so we go ahead and get rid of update and then we will do a void uh on animator ik you can go ahead and delete what what the int layer index is that's not something that you're going to be using so um then we will do if there is an animator i'm sorry then what we'll do is if right hand object is not equal to null then we're going to we're going to do some things so the first thing we're going to do is anim dot set ik position oops set ik position weight and under that weight we are going to do um avatar ik goal if you look if you look at what it says in there void animator dot uh set ik position weight it's asked basically is that's asking for the weight that that was what the zero to one is going to be um so we'll we'll do the avatar ik goal and then we will do dot right hand oops and and then for that we need to set a value which would be a float value as you put that right hand weight we're going to go ahead and copy and paste that into this and that'll set your weight now the next thing that we want to do is do our rotation so it's going to be anim dot set ik rotation weight and basically this does the same thing so all you need to do is the avatar ik gold dot right hand and then we will do the right hand weight so basically that this value sets both of them so that way you don't have two different weights you can make a separate weight if you'd like to but it's i recommend not to but it's completely up to you how you want to do it um you know you might want to do it later on when you start getting into you know different key framing of animations and stuff like that but after we've set the position and the rotation weights we're going to do an anime anim dot set ik position and under this position we're going to do an avatar ik gold dot right hand and then in the right hand it's going to be right hand object dot position so wherever you're moving this position that's where it's going to be um that's basically what that means so next thing you're going to do is adam set ik rotation same thing avatar ik goal dot right hand and then we're going to say right hand object dot rotation so now basically what this is doing is it's getting the um the position and the rotation that you set the ik to so if we go back into unity let this compile click on our player scroll down to the bottom and just pop the ik controller on top next thing that we need to do is we need to actually get some weapons so i'm gonna go into my app's asset folder uh if you don't have any weapons yet um do do look for some there is some for free on the asset store that you can find um but that's that's completely up to you on how you want to do it you can model one real quick or you can throw one together with cubes or something like that but for right now i'm gonna go ahead and i'm going to get the we're going to use an assault rifle let me just find a good picture of one and i guess i'll just use the scar right here so the scar i'm actually going to go ahead and under the player i'm just going to drag and drop this right onto the player object um and then we're going to move it up the way we want it get it into a good position because eventually you're you're going to be doing a little bit more if you've watched the kiwi coders tutorials you know he he goes into animation rigging and this is going going to be very similar just except we're going to be doing everything with with script and not not just visuals so uh we can go ahead and unpack this if you have this pack but under the under the weapon itself what we want to do is we want to right click and create a new empty object and we're going to rename it to right and object right so the right hand object is placed on the weapon itself so if you hit the player and you grab that right hand object and you place it inside the right hand object in the uh in the animator make sure this is put up to one and i'll show you in a minute why or what that does exactly but now what we want to do is we want to go under the root the hips spine one spine two spine three uh the shoulder elbow and under the elbow um if you're using the miximo rig um it would be the i believe the left or the right forearm but uh what you do is you create a new empty object and rename it to right hand hint and back on the player on the ik script put that hint object in the him variable slot so now everything should work here um let's go ahead and keep it at one and we'll hit play and once once this comes over on let's see what happened here for some reason it's actually not working um i know why next thing you've got to do is you go into the animator go into your layers and then under the uh click to change layer settings under the settings tab uh make sure you do the ik pass you got to take that ik pass and it will pop up an ik here now if we go back into this and we hit play everything should work there you go it's already starting to work now where what i was getting at when you when you have the weight here when you move this it actually moves the the arm as well so basically at zero it's not it's not doing what you want at one it's doing exactly what you want and then if you're right in the middle it's kind of it's kind of trying to do it but uh you can keyframes keyframe things doing it this way if you've watched like i said kiwi code or stuff you you'll be able to understand what's going on here but we'll go ahead and keep it at one and what we'll do is we will click on the weapon get the right hand object and if we move this right hand object now we're manipulating everything that's going on with the animation we can also rotate it to put the hand on the trigger itself so if you back it up just a little bit and then you get this and move it over and i mean you you you can take your time and do this i'm gonna just do something real quick and real messy um but this is this is basically about where you want it it's right on the trigger itself so you you have that going uh the next thing that we're going to do is we're going to go to these three dots still clicked on the right hand object click them three dot dots and copy that component and then you come out and you paste those values now since you have this back down at one when you hit play it's going to go back to that that position and rotation so now if you look everything is still the same now you you'll notice that the elbow is a little bit sucked in um this is why we make a hint when you when you click on that hint and you move i'm sorry we haven't set up the hit yet but um we will be setting this up in just a minute actually so we'll come out of play mode hit save and we will go back into our ik script and under the ik script we are going to go ahead and set up our right hand hint and this one is actually really really fast um i'll go ahead and just make some notes here um that way we know what's going on uh this will be the right hand and actually we i'll do it differently we'll do a region region uh right and ik and then under here we will do um and region that way you know if we wanted to move that out of the way that we know we have it what i like to do is i kind of give it a little bit of room um that's just my preference i like keeping a space between them so that way i can get get to them pretty easily the next thing we're going to do since we have the right hand we are going to do an if right hand hint is not equal to null then we're going to say anim dot set ik hint position oops position weight sorry and then we're going to do avatar ik hint instead of avatar ik goal and we're going to say dot right elbow you can kind of do this the same way if you'd like to and actually what you can do is you can you can do another random range a range between 0 and one for a new weight or just set the weight to one through code and now you don't have to worry about about that affecting anything because once you've turned that weight off of the hand then you don't have to worry about the elbow effecting anything else um so the next thing we're going to do is anim.set ik hint uh position and then we're going to do the avatar ik hint dot right elbow and then we're going to say right hand hint dot position we're going to go ahead and save this script and go back into unity i'm going to wait for it to compile we're going to hit play all right once this pops up we can go over to the scene mode our scene screen i'm sorry and then you've noticed that our elbow is still kind of sucked in if you uh click on that right hint now if you kind of move um there it goes you just got to move the elbow out just a little bit kind of gives it a little bit more of a realistic uh feel to it gets out of the stomach so i'm just going to leave it right there and copy copy that component and just make sure that you paste it back in so when you hit play again then it will go back to that same position now if you look we we have the same thing going on everything looks like it's working pretty well so now what we're going to do is we're we're going to go ahead and save this right here make sure you save your scene frank with me it's a better idea to do so but now now what we're going to do is we're going to do the same thing for the left hand so basically all you have to do you can copy this all the way from the header copy paste it make this left just make everything left that way you have all the same things left hand we will copy left replace it with right i'm sorry replace right with left so that way we have pretty much everything the same and then all you have to do is actually you can take this entire region right here copy it under there paste it and we will actually just change everything over to left instead of just right that way oops control z almost messed up all right instead of right hand object it's going to be left hand object and i'm just going to do this real real quick here here here and here and we're just going to change this to uh left hand this will be copy here paste paste paste this will be your left elbow same thing copy and paste save that script go back into unity wait for that to compile and under the player now we have a whole new whole new system for the left hand ik so what we're going to do is we're going to close down the right clavicle we're going to go back or go into the left and do the same thing we're going to add a left hand hint and then under the weapon we're just going to duplicate this and rename it to rename it to left hand object and under the player on the ik script we're going to go ahead and drop the hint and the left hand target and we're going to hit play now everything should be working if you go back into your scene uh move sorry about my camera work now we move over where we need to be grab that left hand target our object and now we start moving the way we want it to be so if you notice i'm i'm having to twist the the uh arm up a little bit to get it correct put it down like that grab it put it where it needs to be come over here like this basically what we're going to do is we're going to make it look like it's it's holding the weapon um like it as if it is an actual weapon being held you know what i'm saying um so there there's the left hand and uh just make sure that you never ever go out of play mode without copying your components and pasting them back in so if we paste those values and we hit play and go back to the scene mode then we are back where we were when we where we left off the elbow kind of looks a little weird so what we'll do is we'll grab the elbow and we'll move it out just a little bit and we will copy that component come out of play mode and paste those values when we hit play now everything should be working now now you have a weapon set up you can run around with the weapon i know it bounces around a lot right now that will change but now what you can do is you can go into the scene scene view go to the actual player again and click on the weapon so now if you move the weapon it actually moves the hands with it so you put the weapon where you want it let's say we want it right there it kind of looks kind of looks decent i'm not going to lie it looks a little decent so we'll go ahead and keep it there but what you want to do is you want to copy that transform as well and you want to paste that transform so when you go back in everything stays the way you had it in the beginning so now the the stock looks like it's against the shoulder um and we we will fix all the the movement and stuff like that um when we get things done um i noticed that the camera is kind of going a little wonky um let's actually fix that real quick we're going to go up in a hierarchy to the player for the camera itself we're going to change the max district cam max cam distance to two and that should fix no let's try three huh that's something to work on later um turn off my gizmos i don't really understand why it's kind of moving in like that but huh we'll worry about it later um for now we're we're gonna go ahead and just keep it where we're at for right now um i don't know why it didn't change three like i asked it to this is supposed to be one i believe huh yeah yeah we'll we'll work on it later on uh for right now we're gonna just put it back where it was um i actually got a a comment on my last video about it and uh it's something that we will be working on as far as bug fixes go stuff like that next thing that we want to do okay under the weapon itself we're going to add some new components first component is going to be a position constraint and then we're also going to add a parent constraint it's similar to what uh cubic coder does uh we are going to go ahead and under the main camera which is should be your main camera go ahead and throw it at the top now i'm an idiot i was i wasn't in play or i was in play mode go ahead and save that real quick under the under the main camera what we're going to do is we're going to right click and create empty and it's going to be called an aim target we're going to set it 20 meters in front and then we're also going to go to the weapon since i wasn't in play mode we're going to do a position constraint and we are also going to do a parent constraint and then for the weapon itself we'll get over closer to it so we can see what we're doing we will right click and create a new empty um and we will call it the weapon pivot and we'll move this weapon pivot where it's supposed to be somewhere right there um let me look right at it so i know what i'm doing uh we'll grab that pivot go over just a little bit and up it looked like make sure right on the stock of the weapon yeah it looks pretty close so once we've once we have that position then what we can do is we can pull that out and add our weapon under that weapon pivot so now we have a rotation that we can do for up and down rotations and we'll we'll deal with that later on but for right now what you want to do is you want to go to your weapon itself and under all this here what we're going to do is under the constraints you have source objects that you can add in here so if we add a source object here we can actually do the weapon pivot and then we will go ahead and activate that now everything's set up for the weapon pivot if we go to the position constraint what we want to do is we want to add the right shoulder to this so we're we're going to grab the right shoulder which would be the clavicle in this case uh we'll go ahead and lock that down here we'll activate it we'll unlock the uh constraint settings because if not then it'll shoot the weapon up then we will go ahead and hit play now if you run with it the weapon sways a little bit but it now sits against the the player itself so everything everything works well with it you can uh crouch do everything you need to do so now the next constraint that we want to add is we want to add the aim constraint and under the aim constraint we're going to go ahead and activate it go to the constraint settings we're going to unlock it we're going to open up a new one and then add the aim in target so now when we hit play um it should be moving with it let me go back in and lock this hit play apparently it's not moving with it just yet um it should have been cnub i believe it's supposed to be a vector hit play okay um that's something we will work on in a in a later video but now now that we have everything set up you want to take the aim constraint and pull it right above the parent constraint and that may have been our issue let's go ahead and hit play and see what happens no that's something that we'll have to work on later on um let me make sure that it is in front of us it is indeed in front of us but for some reason it's in the ground which don't make any sense to me unless the camera itself is kind of looking down yeah see the camera itself is kind of looking down that's something we'll work on in a little while uh probably next video uh but go ahead and save this scene again we're gonna go ahead and save that scene so that way we we don't have to worry about anything you know messing up on us but now we have a basic weapon uh ik script so now we can we can hold on to our weapon we can do whatever we need to do as far as the the movement goes uh as you noticed it it actually moves the the weapon down there is one thing i do want to try since we do have a little bit of time still is under the root if you go down to the neck we should have that follow target let's go ahead and drop it on the player instead of having it on the head um and we'll hit play and i wanted to see if i could fix these bugs real quick yes it does fix the bug all right so what you want to do is you want to take the look at target that we created from the head and move it onto the player object itself so we're just moving it out of the root uh hierarchy and pulling it onto the onto the player itself so if we leave that there go ahead and leave that there and hit save next thing i wanted to show you guys for a real quick uh thing if you see the pitch clamp here okay so the pitch clamp what this does is it actually changes uh your camera look so let's say i wanted to be negative 45 on the y now on the y i can go up a little bit higher um so all you have to do is is kind of play with these values be negative 20 let's just do negative 20. see how high i can go uh negative 23. yeah that looks a little bit better i'm actually going to change this to a lower value like 45 that way i'm not looking all the way down at my feet so yeah we'll do 45 and negative 23. so we'll change these values to 45 and negative 23. so sorry about that um i had to do some changes for it real quick but uh that's that's basically it for this video uh this is episode four this is like a uh weapon aiming uh we will go into a lot more um again make sure you're saving your scene uh quite frequently now that way you can always go back to what you need i recommend going into git or using collab even if you are by yourself now the reason for that is if you mess something up and you can't fix it then you can always revert back to your your latest version so anyway guys um i appreciate everything you guys do uh watching my videos we're almost to 200 subs and you know we we've been going for you know a little over two months i believe and i'm just flabbergasted i i you know i can't thank you guys enough um but if you haven't subscribed yet please do um hit that subscribe button hit that notification bell um make sure you share this video if you feel as though you want to donate and want to be shouted out on any of these videos my patreon link will be down in the description along with my discord server i do frequently talk on discord so if you need any help or just want to hang out i do have a discord server that you can hit the link you can also go to my twitter account and you know follow me on twitter but yeah guys um you guys have an amazing night again thank you for everything you do uh don't forget to hit that like button if you like the video there's a lot more coming we're going to go into part two of this coming here really soon uh so definitely definitely can't wait to to see what you guys can do um if you have any questions have anything like that like i said you could join my my discord or leave some comments in the in the comment section below and uh we'll deal with whatever issues you're having anyway god bless good night peace
Info
Channel: Final Stand Studios
Views: 1,786
Rating: undefined out of 5
Keywords: unity3d, tutorials, third person shooter, tps, unitytips, unitytutorials, animation rigging, rigging, animation, PUN2, multiplayer, Inverse Kinematics, shooting, aiming
Id: nda3JJhyecI
Channel Id: undefined
Length: 36min 40sec (2200 seconds)
Published: Sat Sep 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.