Godot 3D - Basic Character Controller | Character Animation Tutorial: 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back today we're going to be working on a character controller it's not going to be too complicated of a character controller we are going to go ahead and base it off of the built-in character controller so if you have a character body 3D so if we just go up here to the character base and we'll just change that type over to a character body 3D go ahead and create a new script from this and it will give us a very basic character template so if we just for example call this character controller then when we create it it will have some very basic boilerplate code here and we're actually going to go ahead and use this we're going to modify it to handle the animator and then we'll just worry about actually making it a little bit different later on but we're going to need to go ahead and create a C sharp variance we'll just remove that real quick and create another C sharp variant we're also going to need to create one more pair of scripts and this is going to be a camera controller and this is going to allow some mouse rotation and so we're just going to call this one camera controller and then we're also going to create that same for Godot script so now that we're here we need to do a little bit of setup we need to handle this Collision issue so if we go ahead and add a collision shape 3D and we're just going to set that to a capsule collider and it's in a little bit of a plaid place that's good enough it doesn't have to be exact mind you when you go to actually running around if the feet are just hovering above the floor you may need to adjust this to collider to make sure they actually touch down you should actually see some very basic movement and that movement is inverted as well so what we're going to do is we're just going to go ahead and rotate that player and we're also going to go ahead and rotate the camera 180 degrees this way we've got it pointed the proper direction if you could look at the blue arrow make sure that the blue arrow is pointed away from the direction that you want the player to look I've also went ahead and implemented some new inputs so if we go and look at the built-in inputs we have UI right UI left UI up and UI down was originally just the left right and up and down arrow keys but I went ahead and added was and D so for now we're going to go ahead and hop into the camera controller and we'll do that and get mouse look up and running and then we're going to go into the character controller and modify it a bit to make it work with what we need all right so first we're going to add a couple exports the first one is just going to be a reference to the body node and then this is going to be a node 3D and this is going to allow us to rotate the actual body of the player character left and right and then we're also going to need a float variable and this is going to be our camera pan speed this is just going to control the Delta movement speed of the rotation we are going to need an input override so we'll go ahead and build that out in the c-sharp and the Godot script variant and then within that input override we're going to go ahead and cast the event to input Mouse motion event and this is just going to let us actually use it as a motion event if it is actually a motion event of course then what we're going to do is we're going to go ahead and create a new variable which is going to be a vector 2 or just a VAR in good oscript and that's going to be our Delta and this is going to be the event.relative that's basically the direction and distance moved by the mouse then what we're going to do is we're going to create two floats one is going to be our new y rotation and one is going to be our new X rotation so the Y rotation is going to be using the body node as a base because it's rotating left and right and there's going to be using the Deltas X but also inverting it because that's just the way Mouse motion works and we're going to multiply that by the cam speed then on the new X we're going to take the rotation degrees dot X of the actual camera that's the node that this script will be on and we're going to go ahead and add to it the Delta dot y not inverted multiplied by the camera and pan speed we're going to go ahead and clamp that X rotation and that's going to give us a little bit of a reference so that we don't go completely upside down or anything like that next up we're going to need to go ahead and apply that those rotations so first we're going to apply the body node rotation that's going to be a new Vector 3 and this is going to be with the new y as the y-axis of course and then 0 0 as the x and z and then the rotation of this object or the camera and we're going to create a new Vector 3 and we're going to be rotating its x-axis but make sure to input the y-axis from the current rotation degrees that way we don't rotate the y-axis but it is whatever the original setting of it was that way we keep that 180 degree rotation we did in the inspector and finally we are going to need to put in a couple checks for specifically the mouse so to make sure the the mouse is locked into the camera we're going to first we're going to create the event that actually toggles it and that's just going to be checking to see if if it's action pressed UI cancel and this could technically be done in an else statement but we're not going to concern ourselves with that and then we're going to check to see if the input.mouse mode is currently confined this means it cannot move and it's invisible and if it is then what we're going to do is we're just going to toggle that to an input Mouse mode of visible and just free that Mouse up and then we're going to create an else statement for this and if it is free already we're going to capture it this way by pressing escape or whatever UI console is bound to we can just toggle on or off the mouse mode and then we're also going to go ahead and paste all of that into the ready function to go ahead and lock in that Mouse at the beginning and that'll be pretty much it for this all right so we went ahead and got that in and we can just throw that on here so let's select the camera controller.cs and we're going to set the camera speed up to something like two then we also will need to go ahead and select the character base that way it knows what to rotate on the left and right so let's go ahead and hit play and see what happens now we can look up and down just like in the original tutorial series on character controller and if we move we move in the direction that we're looking so we're just going to go ahead and start work on modifying the character controller so I'm going to kind of do this one at a time because the changes are pretty generic real quick I'm just going to go ahead and ex set these variables to exports and we're going to type these a little bit more strictly just for consistency's sake we're also going to rename these to speed and jump velocity without all of the complexity all right so I went ahead and added a couple new variables we can go ahead and throw in the names the proper names for things again and then what we're also going to do is we're going to create a couple new variables that are going to be the current input and the current velocity as well and then all we're going to do is replace the input dur with that now we can go ahead and create a new process function and then what this process function is going to do is it's actually going to apply that input to the actual animation so real quick let's go ahead and test it with just setting the animation tree directly with the input and then all that should do is just set the animation tree once I've tested this I'll hop over to the C sharp variant do that and then we're going to soften up these animations all right so once we have that we need to go ahead and go over the animation tree and we're just going to copy this property path this is The Locomotion blend position and then you paste it right there so all this does is it takes that path and it sets the variable and that's how you set set variables within an animation tree now transitioning from one state to another is a bit more complex but we're going to go over that later we're not going to go over that in this tutorial but this will work for now so let's go ahead and hit play and see what happens so now is a very hard animation cut now obviously the angles have their own issues but that's a problem for another day but this isn't exactly ideal it's not very smooth let me go ahead and create this in C sharp and I'll speed through that because it's pretty much the exact same as what I did here and then we're going to soften up both of them so firstly what I'm going to do is I'm going to go ahead and set these public constants to actual export public floats that way we can edit them in the inspector we don't have to change the names in this case but we do need to go ahead and create the other variables as well which in this case are a string for a Locomotion blend path just like in the Godot script and we're also going to create an animation tree reference we need to make sure that the animation tree reference first letter is lowercase just so we can differentiate between the base class and this variable next we need to also create a float for the transition speed and these won't be used just yet but we're going to use them as soon as we smooth everything out and then what we're going to do is we're going to go ahead and assign some of these variables and put a getter Etc on each one and then we're going to create two variables which are going to be private for the current input and current velocity and make sure to assign those to Vector 2.0 so that that way it zeros out at the beginning and that way we don't have no reference exceptions because nothing's assigned to these variables the first time around then we're also going to go ahead and replace the input dur with the current input and we're going to go ahead and create that override for the process function and then we're going to just do exactly like we did before we set the animation tree Locomotion blend path to the current input and I'll be pretty much it for the C sharp all right and then we're just going to go ahead and hop in here and set the C to set that to the C sharp script and see how that looks and we once again will have to copy and paste the blend path in all right let's go ahead and hit play and see what happens okay and that works exactly the same what we're going to do next is we're going to go ahead and soften this up and so the way that I currently do it in my personal project as well as other projects I've worked in is I have a separate velocity that is the actual animation velocity and then I have the animation velocity slowly blend towards the input velocity this can result in a very nice smooth blend so what we first do is we create a Delta so all we're going to do is we're going to subtract the what we're trying to get to from what we are currently at just like you would get a vector between two three dimensional points then we're next going to check to see if that Delta length is greater than transition speed times the Delta time this is the amount of time since the last frame and if so I'm going to go ahead and clamp that Delta so we're just going to normalize it and then we're just going to multiply it by exactly that the transition speed multiplied by the Delta and whatever the case may be we're going to add that Delta to the current velocity which once again is the actual velocity that we're inputting into the animation tree so let's go ahead and replace the character controller with the Godot script variant and let's try that on for size though I think the character movement's a little bit slow so we'll just set that up to something like yeah that's a lot better all right there you go that's how you smooth out character animation now I'm gonna go ahead and do it in the C sharp variant real quick we're just going to speed through that all right so I'm just gonna hop down here into the process function I'm going to create a new Vector 2 for a new Delta and this new Delta is going to once again is just going to Simply equal current input minus current velocity that's going to give us a vector from current velocity towards current input then we're going to check to see if that length is greater than transition speed times the Delta cast to a float of course and if that's the case we're going to go ahead and make the new Delta normalized and multiply it by transition speed by Delta that way we can go ahead and clamp the length of it and if it's less than that it's just going to equal zero so it's not actually going to apply any real movement to the velocity anyways then we're going to go ahead and apply that we're going to take the current velocity and add in the new Delta and that should be pretty much it we are going to have to make sure that we put the current velocity into the set animation tree though and we're going to go ahead and test that with the C sharp variant set that transition speed up set that movement speed up let's go and hit play and see what happens oh and I fell off yeah so there you go so that'll be all for this week next week we're going to be working on transitioning from one state to a next so in this case it's going to be jumping so we're going to transition into a falling State and possibly have an animation for an actual jump in between there then we're also probably going to go ahead and tie in a method there to kick the velocity based off of when the actual animation for jumping occurs that way it's a little bit more precise then we're also going to later on possibly work on swapping out a state for just the upper body so perhaps drawing a firearm or something like that but all of that'll be later for now thank you all for watching I hope you all have a wonderful week and we'll see you all back here next week or possibly this weekend if I get that video out in time good foreign
Info
Channel: Bonkahe
Views: 3,162
Rating: undefined out of 5
Keywords:
Id: l4uWdObc4do
Channel Id: undefined
Length: 14min 36sec (876 seconds)
Published: Wed Jun 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.