Character sits in Chair | Unity Animation and IK | #Unity #GameDev #Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi in this video i'm going to show you how to get a character to sit in a chair by the end of the video we'll have this scene here the guy will walk across sit down in the chair look simple let's get going okay so here i am in a brand new 2019 project in unity and the first thing i want to do is import this animation package from github now this is an open source package you can use this to do whatever you want with it it's got a very permissive license and in this installation section it tells you how to do what i'm about to do but basically i want this url here and then if i go back into my editor i go to windows package manager click on the little plus up here say add package from git url paste it in click add wait a few moments so the package is now imported so i can close that down and if i look inside of my packages folder i have this new wizards codes animation and inside of here is an animation folder and what you get is a bunch of animations but that's not really what i'm offering here there are animations in here but they're not great if you just look at this one as one example this is a melee attack i am not a very good animator but there aren't or at least i haven't found good open source animations yet so if you are an animator please donate them make this a much better project if you know of where i can get some let me know and i'll pull them in but they're good enough to get going um what is important here is we have a whole load of scripts inside of here that we are going to use uh in this project to create the sitting character how do we do that i'm not going to go through everything step by step i'm going to show you the various pieces as they come together so that you can reuse what you get in this folder so if you go inside of the scenes folder you'll find that there is a sitting scene and in here we can see we have a chair we have a model and a couple of buttons in the ui like we saw in the intro excellent now we can't actually edit that inside of the packages so what i'm going to do is i'm going to open up my scenes folder here and i'm actually just going to copy that so that i have a copy that i can work with my sitting scene i'll call it just for differentiation so i know i'm in the right space if i hit play here you'll see that uh we have some functionality he's standing in idling but if i hit the sit button he doesn't do anything why is that well let's go and have a look in our console and sure enough we have an error here uh this first one is saying there's no head transform set up look that's disabled we don't really worry about that that should probably be a warning rather than an error um sets destination can only be called on something with an active nav mesh ah of course i forgot to bake my nav mesh and bring up the scene view go to bake hit the bake button and we now have a navigation mesh of course all right so now if we hit play and hits it we run over to the chair we sit ah we don't sit why don't we sit well that is because this package doesn't currently have an animation for sitting contributions welcome so we need to go and get one how do we get one well if we go to mixamo.com and we search for sitting we'll find a whole bunch of them but in this case i'm to go with sitting impatient just one of those that one looks fairly good so we're going to click download make sure we've got fbx for unity selected and then click download again and we're going to save that into our project so i'm going to save as and then i'm going to navigate into my project create a folder inside of here for it so new folder call it animations go inside of there and save that in that folder and when i go back to unity that will be discovered by unity go into my project view there's my animations folder and there is my sitting animation open the inspector and we can see that animation is there okay now we need to apply that to our man in our scene and by default he uses a basic humanoid controller and as you can see here it says override this right the idea here is i create my own controller using my own animations so i go create animation animator override controller call it what i want my controller and then i'm going to set the base animation to the basic humanoid controller that comes with the package and now we have all of these animations that have some default settings but as you saw the sitting idle setting is just a standing animation it's not a sitting animation so let's drop our sitting animation in here and hit play now we hit set runs over and doesn't sit why not ah i remember why not if i go to the miximo import you'll see that the rig is set to generic whereas our guy is humanoid so we need to change that to humanoid hit apply hit play hits it runs over and doesn't sit why is he not sitting it's really quite simple i have not changed the controller over to the one that we created with our sitting animation in should drop that in there like that hit play okay and he sits now it's a little jerky as you'll see now we're going to come back to that later on and we're going to improve on that later on uh it'll be much better by the end of this video all right so next up how does all that actually work well let's take a look we'll start off with the ui because that's what's actually triggering these things we go down into the ui we can find that we have these two buttons here and scrolling down we see that they're tied to the sitting behaviors on the sip method which is part of the test ui so we'll open that up this method here just calls out to a controller until it tells it to sit at the sit position what's the sit position well that's defined inside of here and you can see that that is at an interaction position which is part of the chair now how do why is that important what we could do quite easily is create an on click on the chair that passed in this interaction position and that can then be used for all sorts of things so this could be a coffee machine for example and if the character is going to pick up a coffee or they could go and stand at the interaction position then get the coffee what they're going to do here by interacting is they're going to sit on it so what actually happens there we go into this controller which is on the character and we see we have a sit which takes a sip position which is a transform because it needs a rotation and a position and the first thing we do is we call move to the sit position and there's a bunch of callbacks there's three callbacks so we'll not worry about what this callback does for the moment let's just go straight through to the move to object uh method and you can see that this instructs a character to move to a defined position and optionally make callbacks at various points in the process and the callbacks are we have the position not callbacks the position we're going to move to we know about the first one is the arriving callback the arrived callback and the stationary callback so that's as you're getting close once you've got there and then once you've stopped after having arrived those are the three steps that you can take in each of these so what we do in the move to method is we set up the callbacks inside of the connector that's the nav mesh connector we'll have a look at that in a moment and then we tell it via the nav mesh to move to a position so this is just standard nav mesh agent uh work so what does this connector do well that is if i go through to where it's defined you can see it's this nav mesh connector what's that let's go through and have a look the nav mesh conve the nav mesh connector converts a nav message a nav mesh agent's movement to parameters for the animation controller right so i'm not going to go through in this in detail and it's actually easier to see if we look at it inside of the project itself um so if we have a look at our character here we can see that the nav mesh connector is here and we have a maximum speed a normal speed factor so that's of the fraction of the maximum speed that they would normally move at the arriving distance is used when calculating are you arriving so we then when you're within 0.2 meters of the destination then it triggers that arriving this that arriving callback the animation parameters they are used to control the motion inside the animation controller we'll take a quick look at that in a moment and then the debug is the click to move that we've been using uh at serious points of this video so what are these parameters well if we go and look at the controller itself i'll go to the base layer and we can see we have the forward and turn parameters here okay and those both feed into a blend tree inside of the movement which he used to make the character move about okay excellent and then we also have this sitting parameter but we've not seen how that is used just yet so where does that get used let's go back to our code and we're going to go back to our controller sit i'll go through to our move to again and remember we have these callbacks the first callback here is going to say well turn to the sit position rotation so that's going to make our character turn to face the correct way in order to sit down on the chair when we are so that's when arriving so just before we get there we're going to start turning the next one is arrived and the last one is stationary we're not passing in a callback there so it won't do anything so this middle one is when we have arrived what happens here well we're setting a bit of an offset to the sitting position why is that well that's because each animation is slightly different and the precise positioning of the character uh is dependent on the animation so this allows us through a parameter that's set in this sitting behavior to create an offset um to make sure that the character actually sits on the chair we'll look at that in a few moments and then we're going to call another move to this time sending in the new position which has this offset uh null for the first two callbacks but on the final one when we're stationary we're going to set ik to active so true is act ik active true that's a parameter inside of this behavior and then we're going to set the boolean sitting to true all right so let's take a look at this ik and the offset positioning let's go back to our project and here's the humanoid controller which is what we just had there and we here we are exposing is ik active and so that will enable us to look at what's happening and here is our offset let's actually make that to zero so if we didn't have that this is what it would look like hit play let's just maximize that hit sit and you can see he's not actually sitting on the chair because we changed that uh offset so let's put that back again and now when we play hits it he moves back and sits onto the chair because of that offset so you're going to want to tune that offset for the specifics of your animation that you're using and so what about that ik well i'm going to leave it in play and just bring it back i'm going to turn ik off and watch his feet let me just zoom in in the scene view and you'll be able to see it a lot better watch his feet specifically see how they've dropped down into the floor that's because again the animation in this case it's not a great animation and depending on what model you're using he could drop down into the floor or he could be have his feet up in the air there's all sorts of different things that can go wrong here and we want to make sure that his feet are actually on the floor placed firmly on the floor and that's what our ik is about here so if i turn it back on again you see he picks his feet up and keeps them on the floor so how do we do that well if you look at our chair you'll see there's a left foot and a right foot position right so let's move let's move the right foot one forward like that and when i hit play and ask him to sit again watch his right foot see he's got his right foot forward how's that happening well let's go back to our code again remember we were setting ik active inside of here to do this and how does that work well let's find here is an on animator ik method and it's saying if the ik is not active then return don't do anything but if it is active then make sure that our right foot is taking the position where position and rotation of that right foot positioning and likewise the left foot and so what we can actually do is we can change the sitting position of our character simply by moving those objects let's go back to be able to see it in the scene view hit the sit button and i can move this about and you know some things don't work it's gone through the chair there but you can adjust the sitting position however you want we could make him rotate his foot outward a little bit okay now i'm using the simplest possible ik here in order to make this happen okay so what we want to do is to be able to uh click on the chair and have our character sit on that chair so we're going to go into our assets folder and create a scripts folder and we're going to create a new script we'll call it um chair interactable okay and let's put that in a namespace wizard's code spike spike is just something that i don't intend to keep this doesn't actually belong inside of the animations package so i'll put it in some other package in the future for now i'm just going to show you how to code it okay and we're gonna use on mouse down and to start off with we'll say debug log chair clicked all right good put that script on our chair the chair already has colliders on it i believe oh it doesn't so i need to add a box collider i think will just be fine just needs to be a trigger we don't want it to get in the way and let's make it it's already the right size excellent so hit play and when we click on it we see in the bottom here chair clicked excellent now it actually took it as a move there which is with something we don't want to do we can fix that by going to the neo and turning off this enable click to move so now i can click and the character won't move see okay all right so now we are clicking so what we want to do is we want to do the same thing that we're doing inside of the ui script that we have here we want to call controller sit position okay but in this case we already know the interaction point because it is a part of this object so i'm just going to set that up in the field so we can allow it to be somewhere else in a different part of the structure but by default it's going to be pointing back to itself so interaction point and that is going to be interaction point position okay and where is this controller going to come from well if we go and look at the sitting behavior we see the controller is a humanoid controller so let's put that in here two there we go we have both our needs to be imported they need the using statement to bring that in don't need that to be public ah it's not compiled oh it takes a transform not a position so i should just do that so now it compiles okay so we we're good there all right and so now we can go back to our project so now i just need to set these two pieces up so let's set the interaction point for this object and in this case i'm just going to drag the human in here but we could use a script to assign that at run time but we're just taking the easy route here moves over turns around sits down simple as that so we can do a lot of improvement to this we can fine tune the animations and put some transitions between positions and i will indeed be improving this open source project as i continue to work on it and i'd love you to help me too so download it use it contribute to it we'll all be better together thanks a lot bye-bye
Info
Channel: Wizards Code
Views: 14,613
Rating: undefined out of 5
Keywords: unity game development, game development, game dev, gamedev, unity, unity3d, indiegamedev, indie game dev, indie game development, unity asset store, wizards code, Animation, Inverse Kinematics, IK, how to, unity 3d, unity animation, unity 3d tutorials for beginners, unity tutorial, unity animation controller, unity animation 3d, unity animation script, game development unity, unity tutorial 3d, unity tutorial for beginners, unity (software), unity 3d animation
Id: bRIpwQUUN24
Channel Id: undefined
Length: 19min 37sec (1177 seconds)
Published: Tue Jan 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.