Blender to Godot: Rigged Character Workflow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Saved! Which rig are you using? Is it made by you?

๐Ÿ‘๏ธŽ︎ 3 ๐Ÿ‘ค๏ธŽ︎ u/tuposacp ๐Ÿ“…๏ธŽ︎ Jul 29 2021 ๐Ÿ—ซ︎ replies

Thanks! Very important information!

๐Ÿ‘๏ธŽ︎ 2 ๐Ÿ‘ค๏ธŽ︎ u/lexpartizan ๐Ÿ“…๏ธŽ︎ Aug 02 2021 ๐Ÿ—ซ︎ replies

To get the exact name of an AnimationTree parameter, just hover over it in the inspector. The tooltip was actually visible in the video for a split second.

When crouching, probably something should happen with the collision shape(s), so that it makes a difference gameplay-wise and not just visually.

Also, using $ is equivalent to calling the get_node function, so if you need the node often (especially if you need it in _process or _physics_process) it is better to do the searching just once and store the reference in an onready var animation_tree = $animation_tree and then use the reference instead.

๐Ÿ‘๏ธŽ︎ 1 ๐Ÿ‘ค๏ธŽ︎ u/G-Brain ๐Ÿ“…๏ธŽ︎ Jul 30 2021 ๐Ÿ—ซ︎ replies
Captions
hi welcome to cg dive this video is for you if you want to export rigged and animated characters from blender to the godot game engine if you don't know which file format you should use or if you tried and failed to export characters from blender to godot i'm going to outline a workflow that will give you predictable results on top of that i'm going to cover how to set up your character ingodo is a third person character this video is part of a long series of videos that i call bridging the gap and i mean the gap between blender and game engines especially when it comes to animated characters or skeletal meshes until you understand what a game ready rig is this gap can be very confusing and people blame blender or they blame the exporter or they blame the game engine and each of those is part of the problem but what solves around 90 percent of all problems is getting your rig ready for export so that will be one of the main topics that i'll touch on here we even created an add-on which makes creating game rigs very easy and i'll share it with you then we'll cover export and import settings and from there it should be smooth sailing so far i've spent around six months on this series and i'll keep making content around these topics we still have to cover retargeting in blender root motion and other cool stuff if you want to support me you can buy the bridging the gap videos on gumroad or you can become a patron on patreon that way you'll get my videos earlier than the youtube release sometimes it's a day earlier sometimes it's a month earlier but either way you'll be the first to see the videos by the way i want to say thanks to juwan who is a godot enthusiast and he helped me a lot while preparing this video follow him on twitter where he tweets about godot news and other cool stuff alright let's start i'm going to start in blender with this rigged character it is rigged with rigify i have a ton of tutorials on my channel about rigging with 3dfi check them out but rigify is not the point here you can have the character rigged with a custom rig that's fine and it is going to work exactly the same way as long as the rig deforms the character correctly in blender it will do the trick just make sure that it doesn't rely too heavily on bendy bones bendy bones are blender only feature and they won't be exported i also have animations on this rig and i made similar videos about going from blender to unreal engine as part of that workflow i showed how i made these exact animations so i don't want to repeat the exact same thing twice if you're interested please watch that video and that will work perfectly for godot as well and so the animations that i have are a wok a run cycle a jump animation idle animation crouch walk action crouch idle and a special stretch animation which stretches the arm of this character so the first six actions are simply animations which are very common in games walking running jumping and so on and the stretch animation is a special one its purpose is to show that we can achieve squash and stretch in the game engine which tends to be a problem if you don't know how to set it up i have a separate video dedicated to the topic of squash and stretch you can check that out as well so this rig that we have on this character is what we'll call a control rig let me rename it as well if you only intend to animate in blender then this control rig alone is all you need but when we want to export to a game engine we need to make sure that the rig is game ready in other words we need a game rig so i spent like a couple of months researching and testing and i came up with a workflow which allows me to make any control rig game ready very easily and even better an add-on developer called blenderboy made an add-on that automates the whole process it's called game break tools or grt for short the add-on is pay what you want so if you can leave us a tip if not get it for free it's all fine but i think the add-on will save you a lot of time and effort and to get the most out of it i still recommend that you go back to my manual workflow videos and watch them that way you'll understand exactly what the add-on does it's an automation of these exact manual once i have the add-on i can go to game rig tools and click the generate the form rig button and that will generate a game rig for me i'm going to rename it to game rig or you can give it any name that you like and i'm going to use the default settings we made it so the default settings give very nice results but there are all sorts of additional options that you can tweak to achieve whatever results you desire when you get the add-on you'll also find instructional videos and in there i show some of the possible workflows so i'll click ok here and the game rig was generated for me this game rig is extracted from the deformation bones of the control rig and also it is constrained in a special way to the control rig splitting new armatures like this into control rig and game rig is not absolutely necessary this is just the workflow that i like and i recommend especially if you're making your own rigs you can keep everything in a single rig and again if you watch the first couple of videos of my bridging the gap series you'll learn how to do that as well but here i'm going to go with this setup before we move on there is one step that game rig tools cannot do for us and that is checking and fixing the hierarchy of the game rig hierarchy is very important for game rigs i explained that in more detail in previous videos one of the big advantages of having a separate game rig is that you can change the hierarchy easily there is no one single correct hierarchy but we are going to go with one of the most common ones it's a logical hierarchy in which each bone is parented to the previous one so to check the hierarchy of my rig i can use the mute constraints feature of game rig tools and that will mute these constraints that connect the game rig to the control rig and now i can freely move the bones of this rig and check if they're disconnected somewhere so for example this collar bone is obviously disconnected and the legs are also not connected to the spine so i have to go to edit mode turn on x mirror and then i'll parent my arm to the clavicle ctrl p keep offset clavicle to the chest and the leg to the spine now if i go to pause mode you'll see that this bone is the top of the hierarchy and it moves everything with it the clavicles are now connected to the chest and the arms are connected to the clavicle and now i'm going to unmute the constraints to re-enable the connection between game rig and control rig so with this workflow the next step is baking the actions from the control rig to the game rig that is actually one of the main disadvantages of this approach if you keep everything in a single rig then you won't have to bake however in game break tools we have the action bakery which makes this baking process very very simple so with this add-on action baking is no longer a problem it's no longer a disadvantage all i need to do is go to the action bakery and tick all of the animations that i want to bake in this case that is all animations on the control rig which i have named with the prefix ctrl then i'm going to select my control rig and my default brick which is my game rig these options i can leave to default and then i'll press bake action bakery in this menu i have the option to rename the baked animations this works exactly as the battery name function in blender so i'm going to rename from ctrl to absolutely nothing in the next field and that will basically remove the ctrl prefix from my actions i'll press ok and now all the actions from the control rig were baked onto the game rig and push down in the nla and if you don't know how to work with the nla i have a video about that as well the main function of the nla is to mix animations but when it comes to exporting characters to game engines it also allows you to organize the animations that you want to export so the rig preparation is actually done and we are ready to export rig preparation can be very time consuming but but thanks to game break tools it went very smoothly so now we can start working on export and import to demonstrate things i will also want to import into godot so let's start the engine and create a new project here i have the project manager i'm going to start a new project give it a name click create folder the renderer settings i'm going to leave to opengl 3.0 and this is something that you can change at any time in good old so don't worry about it and i'm going to press create and edit so here's the engine from here i can change from opengl 3 to 2 at any point i organize my workspace so that i can see both apps and now in blender let's talk about export first there is something related to my workflow if you use two rigs like me make sure that the connection the constraints between the game rig and the control rig are disabled you want all of these eye icons to be disabled for all game bones and when i baked with the action bakery gamer tools automatically disables the constraints for me so i'm all set or again with game rate tools i can click mute constraints and that will also mute all constraints so unmute activates the constraints and mute deactivates them for the whole rig godot can work with several file formats but the one that is officially recommended is gltf so we are going to stick with gldf i tested it and it works well but there are a few problems there there are a few things to keep in mind first when it comes to exporting your animations make sure that they are organized in the nla exactly like you see here you want exactly one action per track if you have two actions on the same track like this they won't be exported correctly or they won't be imported correctly i'm not sure where the problem lies but it doesn't work so i'm going to undo and i'll have one action per truck the action bakery organizes my tracks exactly like this so i'm all set another problem with the blender gltf exporter at least for now is that it will export any amateur object that you have in the scene no matter how you set up the exporter apparently this is a known issue and it will probably be fixed eventually but for now we have to deal with it so let me show you what happens and what we can do to fix it so in the scene i have my character mesh i also have the game rig the control rig and the meta rig from riggify and what i want to export is the game rig and the mesh so to demonstrate what happens i'm going to add some random meshes in the scene maybe a monkey and a sphere and now i'm going to select my mesh and the game rig and i'm going to attempt to export it so i'll click on file export gltf under include i'm going to enable limit to selected objects and that should cause that i only export the selected objects and all unselected objects should be ignored but that is not exactly what happens the other options i'm going to leave to default apply modifiers can be useful but my recommendation is that you actually apply all your modifiers manually before you rig your game character modifiers are meant for use in blender not for export under animation keep group by nla track checked i tested different configurations and if this option is not checked i don't get the animations in godot so again make sure your actions are organized like this and check group by nla track the option below it can also be important if you're using a single rig then you should check this option but with my workflow because my gain rig only consists of deforming bones this option doesn't matter i can actually check it or uncheck it it doesn't matter the whole rig will be exported and that is fine so with that i can name my character and export it to import it into godot all i need to do is grab this file and drop it into the file system this also important materials for me this character has four materials and they will work fine for me we could tweak the materials but i want to keep this video focused on the skeletal mesh so these default materials will work fine i'm going to double click on the character glb file and choose new inherited and here right away you're going to see the problem that i mentioned even though the meta rig and the control rig weren't selected for some reason they were imported if i try to delete them here godot won't let me so we need a way to fix this so we don't get this needless data in our game file also notice that suzanne and the sphere were not imported here in godot so the selected objects option works for meshes but not for armatures so there are two approaches that i found that can fix this the first one was actually suggested to me by juan who i mentioned earlier and what we need to do is rename the objects that we don't want to import into godot with a special tag so i need to add dash no imp which means no import to all objects that i don't need so the control rig and the meta rig i'm going to rename with this suffix dash no imp now let's select the mesh and the game rig again and now i can actually override the file that i already imported into godot i'm going to right click here in the file system and choose show in file manager i'm going to copy this path here and then export gltf in blender paste the path and and choose the character glb file and the settings i'll leave as they were click export and when the export is done and i go to godot the asset will be re-imported now these changes won't be visible here in the instance i created so i'm going to close it without saving it and open a new instance and you'll see that the meta rig and the control rig are no longer in this hierarchy so that's nice however if i click on the animation player here and i look at the animations that were exported you'll see that the control actions the actions for the control rig are still here so we have more work to do back to blender i'm going to find the actions for the control rig and delete all tracks and export the character again same settings and again i'm overriding the character in the godot folder and now back to godot the character will be re-imported i'll open a new instance select the animation player and now i only have my game animations and not the control ones so that's great that is a perfectly clean result the other way to achieve a clean result maybe even a little bit cleaner is to actually clean up your blender scene so first i'm going to save my scene to make sure that i can always go back to this state i'm simply going to override the current file and then i'm going to save another file save as and i'm going to call my scene something like export and here i'm going to delete all objects that i don't need so the control rig i don't need these meshes i don't need and the meta rig i'll also delete okay now with a scene that is so clean i can simply go to export as gltf and it doesn't even matter if i check this checkbox either way i'll get a clean file so let's override it again go back to godot open a new instance and here is my clean file with clean animations so let me switch to the idle animation and let me show you the last problem that i noticed with the exporter in blender if i go to the 3d view and go to front autographic view i can see my character's face however in godot if i go to front view i see the characters behind again this is a known issue supposedly it has to do with how opengl renders stuff but honestly as a user i don't care i don't care about opengl i have my character in blender facing front and i want to see the same in godot so hopefully the developers of the gltf exporter will do something about it i can imagine having some sort of option here in the transform section that fixes this for now i don't have a perfect fix for this one thing that you can do is choose your character go to transform and rotate it 180 degrees on the y-axis and that should work although it may cause problems in certain situations but in this case i'm going to keep this orientation of the character and that is going to work for us even though it may not be ideal okay now i want to set this guy up as third person character inside godot player characters generally need to be set up as kinematic bodies and to do that i'm going to select my character glb file glb is the gltf extension and go to import and here root type is currently set to spatial i'm going to click here and set it as kinematic body and click now if i go to my scene my character is still set as a spatial as we can see from this icon but if i close this instance without saving it and open the character again it will be set as kinematic body now i'm going to save this so called scene in godot and if you had already saved the scene before changing to kinematic body then you may need to close and open that scene and then you should see the character change to kinematic body but anyway i'm going to save the scene here by pressing ctrl s and i'm going to save it as character tscn if you're not familiar with kudos philosophy each element in the game is saved as a separate scene so even a single character will be a scene and then this character scene will be instanced in a level scene or a map scene we're going to create a simple map scene soon and you'll see how that works actually i'm going to maximize the godot window now and here you can see that godot is giving us a warning about this character and it says that it needs a collision shape so let's select the character press on the plus button here and look for collision and choose collision shape create for the shape of the collision i'm going to choose a capsule rotate the capsule 90 degrees on the x-axis move it up make it thinner and then play with the height so that the capsule more or less touches the ground yeah that should work so press ctrl s to save the character and then i'm going to create a new scene and that will be our world or our map i'll make it a 3d scene i'll rename it here world for example and to give the character something to stand on i'm going to press the plus sign look for mesh and select the mesh instance under mesh i'm going to give it a plain mesh and a transform i'm going to scale it 10 on x and 10 on the z axis so that it is a little bit wider and now i want my character to be able to collide with this plane with this floor so to speak so with this mesh instance selected i'm going to click here in the scene on mesh and choose create static body and that is just a shortcut that creates a static body and a collision shape okay now i want to add my character to this world scene first i'm going to save the world scene actually ctrl s and call it world tscn and now i'm going to select the world node here click the link icon and choose the character tscn file and open it and now my character is in the scene this is this character here any changes that i make in the character scene will be reflected on this character in the world scene because it is an instance of the character scene so now i have a character in a simple world for the character to walk in so if i try to play the game now first godot is going to ask me to select the main scene for this project this is the scene that will be played first when you start the game so click on select and i'm going to select the world tscn file and open it and now we'll have a preview of the game and it is just a grey screen that is because we don't have a camera in the scene and because we want to create the feeling of a third person game the camera will have to be added in the character scene so let's go to the character and i'm going to add a camera to this character but before that i want to add another object that will serve as a pivot of that camera so i'm going to press the plus button here and by the way if you extend spatial i just noticed this but there is something called a spring arm and this node is actually specifically used for third-person cameras however i haven't explored it so in this case we're going to just create a spatial node and call it camera pivot and with this node selected i'm going to press plus again and search for camera and i'm going to add a camera in the scene again something that you may want to explore is this clipped camera it is supposed to create a camera with automatic collision detection which is very nice but i couldn't get it to work so i'll go with a normal camera now first i'm going to select the camera pivot and move it around the chest area of this character and then i'll select the camera and you'll see that it is pointing in the opposite direction that is because of the gltf uh export problem that i showed you earlier but i'll simply rotate this camera 180 degrees on the y-axis and then i'll move it into place behind the character i can go to view and split it into two and one of them i'll make into a preview something like this and now if i play the game i can actually see my character in the scene but we cannot do anything in this scene so let's start setting up the interactions so one thing that we can set up very very easily is the default animation that will be played as soon as we start the game if i go to the animation player for my character and i'm going to choose the idle animation if i press this a button here then this animation will become the default animation that will be played as soon as i play the game and also i want this animation to be looping and here we have a little looping icon if i click on it and i click play here this animation will loop however if i start the game my character will play this idle animation once and then it will freeze so unfortunately as far as i can understand there is no simple way to set an animation as looping inside godot you can do it through code there are a couple of different ways but probably the best one is to name your animations so we are going to go back to blender and we are going to do another gltf export so what i need to do is to rename any action that i want to loop with the dash loop suffix probably the easiest way to rename actions is in the outliner i'm going to switch to blender file expand actions and here i have my actions and for example the idle action i want to be looping so i'm going to add dash loop to it okay and i'll add dash loop to all actions that i want to loop so everything aside jump and stretch needs to loop and now i'm going to select my character and armature and export this gltf again i want to override the character in the godot folder again and now back in godot the character will be reimported now if i go to the animation player the animations will still have the old name so i'm going to save the character with ctrl s close it open the character tscn file and the animations are still not visible so i'm going to actually close godot and start my project again and now if i open my character tscn file the animations have been updated so i'm going to select the idle loop animation make it default animation and now if i play my idol animation will be looping so that's it i wish there was an easier way to set animation says looping right inside godot but at least for now i don't know of such a way so now it's time to make this character move inside the map and i'm going to try to do that using a script so let's select the character right click on it and attach a script the default options are fine so i'll click create i'll get rid of all comments although i'm going to need the process function the first thing that i'm going to try to do is to get the character to move in the map front and back left and right so for that i'm going to need a velocity variable equal to vector3.0 and another variable move speed that i'm going to set to a default value of 250 i found that to be a good value but later we are going to create a system to change this value depending on whether the character is running or crouching or just walking next we are going to need inputs that we are going to use to move the character and godot gives us a nice and easy way to set up inputs from project project settings go to input map and here i'm going to create new actions i'm going to create one called front another one called back left and right for front i'm going to click the plus sign choose key and give it a w key so i'm going to go with the wasd keymap that is common for pc games back s left a and right d okay and close the project settings now in function process i'm going to create a new variable called horizontal horizontal input equal input dot get action strength and then choose front and then minus input get action strength back so there are different ways to do this but here i'm creating this value that will be positive when i press the w key and negative when i press the s key similarly i'm going to create a vertical input variable and then i'm going to create one more variable called normalized input it will be a vector3 and for its x value it is going to take the vertical input y will be zero the y axis is basically jumping we're going to handle this separately and then for z it's going to take the horizontal input and then i'm going to type dot normalized if we didn't normalize the vector our character will move at double speed when we walk diagonally so if we move forward and left at the same time the character will move at a higher speed but this won't occur when we normalize the vector next i'm going to set my velocity value to be equal to the normalized input multiplied by move speed multiplied by delta and now i want to call a special function move in slide that comes with kinematic body and it is best to call this action from the physics process function so let's add a function physics process and in here we're going to set velocity to be equal move and slide and in the brackets i'm going to add velocity vector3 up and now i can actually test my game if i press w my character moves forward as backwards a left the right next i'm going to set up jumping so let's go to project project settings input map add a jump action give it the spacebar key and to make it work i'm going to need another variable a gravity variable and i'm going to give it the physical value of minus 9.8 in the process function i'm going to say that velocity dot y plus equals gravity multiplied by delta so that is not yet the jump behavior that will just make the character fall to the ground and we also have to add this to the to the final velocity calculation so i'll add a new vector3 of zero velocity.y and again zero and now for the actual jumping i'm going to say if input dot is action just pressed jump then velocity dot y plus equals and here i have to set the value for my jump height so something like 5 is actually a good value but we may want to tweak this value without editing the script so i'm going to add a new export variable called jump height and set it to five and here instead of just five i'm going to i'm going to set velocity.y to be plus equal jump height now if i play the game i can press spacebar and the character will kind of jump however if i press jump against paint bar again while i'm in the air the character will keep jumping so we need to find a way to figure out if the character is grounded and kinematic body has a special function for that but i found it a little bit unreliable so we're going to do it another way i'm going to go to my character select the character node press the plus button and i'm going to look for raycast and create it and now in front view i'm going to move it up and a little bit to the side actually to so that i can see this white line which is actually the ray that is being cast and i'm going to make sure that this white line goes a little bit below the floor and this visual raycast node is really wonderful in other engines like unity using raycast in code can be quite intimidating for people who are not very good at coding but with this node is really visual and easy to use so i'm going to go back to the script and i'm going to create a new variable called grounded and and i want it to be a boolean value i'm going to give it a default value of true and here before the jump i'm going to say rounded equals true if dollar sign raycast dot is colliding else false so this is a short notation for the if statement it just says that if my raycast is colliding with the ground then the value of grounded is true and if it is not colliding then it is false and by the way the dollar notation is something else that i really like about godot it allows me to access any note that i have in my scene in my character scene in this case very easily and then i have to add something to my jump statement i'm going to add double and sign grounded so if i press the space bar and i'm currently grounded then the y velocity will be changed let's give it a try and it's not working and that is a very common problem i'm going to go back to my raycast node and enable it it is not enabled by default play the game again and now i can jump and while i'm in the air i can keep pressing spacebar but i won't jump again i'll only jump if i'm already on the ground next let's try to set up the camera behavior first i'm going to need additional variables for the camera x rotation and y rotation and now i want to get access to the mouse movement during game and i can get that from the input function in this function i'll type if event is input event mouse motion then camera rotation x will be plus equals plus equals minus event dot relative relative dot y and camera rotation y plus equals minus event dot relative dot x and you'll notice that the camera x rotation is equal to the y movement of the mouse and that can be a little bit confusing but it actually makes sense the camera x rotation is the up and downs rotation of the camera and the y mouse movement is the up and down movement of the mouse and similarly the y rotation is the sideways rotation and x is the sideways movement of the mouse so now in the process function make sure that you unindent this if statement so that you start a new statement and i can type rotation degrees dot x equals camera rotation x and rotation degrees dot y equals camera rotation y let's give this a try and it seems to be working but something is really really wrong so let's go back to the script the problem is that with this code i'm rotating the whole character at the base note and that is fine for the y rotation for the sideways rotation but for the x rotation i want to rotate around this special camera pivot node that i created so i just need to type dollar camera pivot dot rotation degrees x and let's give it a try now when moving the mouse sideways the character and the camera rotate sideways and when i move the mouse up and down the camera only the camera moves up and down and that is nice but the camera movement is way too sensitive way too fast at the moment so in the script i could multiply these events here by a small value like 0.1 or something like that but again let's create a variable that can be tweaked by the user call it mouse speed and i found that a value of 0.2 works well actually so in here i'm going to multiply by mouse speed and also on the next line now if i play the game the camera is much less sensitive however there is a little problem what i want is for the character to move in the direction where he is pointed currently but instead it moves in an unpredictable way what is happening is that the character is moving in world space rather than local space and as far as i could understand that is because i'm currently trying to manipulate this top note all of the lower notes would probably directly move in local space but anyway there is a way to fix this in the script under velocity i'm going to add transform dot basis and multiply the previous statement by transfer basis and with these settings my character will move in a more predictable way exactly in the direction where he is pointed a small problem with the camera is that if i move it too far down it will clip into the ground so in our script in the input function i'm going to add one more line camera rotation x equals clamp camera rotation x minus 25 to 60. so exactly as it sounds clump limits the values of camera rotation x to these values that i said here and i found these to be nice values of course you can also expose these values here by creating new variables but for me these values will do and now my camera won't go into the ground the last thing that we need to do before getting into animations is to set up the speed of the character when he is running walking or crouching so for that i'll create new inputs one called run and one called crouch and walking will just be the default state so i don't need a special key for it so for run i'm going to use the shift key and for crouching the ctrl key and now in the script first i'm going to need a couple of new variables i'm going to expose these variables as well so i have run speed equals 500 walk speed 250 and crouch speed 180 then i'm going to set another if statement that will handle all of these cases so if input is action pressed run then i could just say that the move speed will be equal to normalized input dot length multiplied by the run speed so that will affect the move speed and the move speed will affect the velocity however that would change the move speed in an instant and that may not be what you want it's nicer to put that in a lerp function so lerp the lerp function creates a smooth transition and the transition is from the current move speed to the new speed which is the run speed and the last bit is how quickly the change happens and i found that 0.05 is a nice value then i'll say elif is action pressed crouch then again i'll actually copy this whole line and then i'll just replace run speed with crouch crouch speed and then else again paste this line and replace with walk speed [Music] now if i play the game by default i'll be walking if i press shift my character will start running i'm not sure if you can see it but the movement is faster and if i press ctrl even though my character won't visually crouch he'll be moving slower so that's it about the physical movement of the character inside the map next we're going to set up the animations so that he doesn't look like a chest pawn to create the complex blending between animations that we are going to need for this character we are going to need another note and that is the animation three note not the animation tree player you'll see that it's deprecated so use the animation tree and create it it has a warning sign so we need to set some things up first the tree root i'm going to go with animation note blend tree and each of these has its purposes but for a 3d character like this one the blend tree will work nicely and i also need to assign the animation player so click on the assign button and choose animation player ok and here our animation tree became active and visible in this area and it is still warning us that this animation tree is inactive so just like the raycast i need to activate the node before i can actually use it and the last warning that we have is that nothing is connected to the output so i'm going to right click here and choose our first node a blend space 1d and right away let's connect it to the output so that it stops complaining and with this plane space i'm going to handle idle walk and run animations so i'll call it idle walk around a blend space 1d can mix one or more actions linearly depending on the float value so if i open the editor you'll see that we have a progression from -1 to 1. in our case the value that will be driving the actions that we'll add here in a moment will be the velocity value so at this end i'm going to want a zero over here i'll have my idle animation and i want it to play when velocity is zero and then as we go in this direction we'll start seeing a walk and then all the way at the end we'll see the run animation so first we need to figure out what our velocity values are so i'll go to my script here and in the process function i'm going to add a new line print velocity dot length so velocity is a vector 3 and the length method will show us the actual speed at which the character is moving so let's play the game move the character and watch the output and you'll see that when i'm walking i reach a value of around 4. if i press shift the value will go up to 8 or so and if i press ctrl for crouching the value will be around 3 okay so back to my animation tree i'm going to set the maximum value to 8 and now at the beginning at 0 i'll choose the create points tool and at 0 i'm going to add animation idle loop then i'll click around the middle of this blend tree and add my walk animation and at the end i'm going to add my run animation okay now if i click on the animation tree there are some parameters here and you'll see my idle walk run node and it has a blend position parameter you'll see that my character is idling here if i change this value to around four he'll start walking and then as i go past four he'll start running okay but what we want is to change this through the script and for that let's scroll down at the end of the process function and i'll add a comment that here i'll be setting animations and i'll type dollar tree animation tree so i am accessing the animation tree node dot set and then in the brackets is where it gets a little bit complicated we need to type a string that points to this blend position parameter and that is not very intuitive but for a string we need quotation marks so between the quotation marks i'll type parameters slash then i want the idle walk run name so i'll copy and paste it here notice that in here it is typed with spaces between the words but what you really want is exactly the way you typed it in here and then slash blend position with an underscore between blend and position so i really wish that godot would make it easier for us to access this parameter rather than having to guess uppercase and lowercase underscores and so on but at least for now this is how we do it and after the string i'll type comma and then velocity dot length so what i did is i set this blend position here to be equal to the velocity length so let's play the game and see what happens the character is idling if i press w nothing is happening and there we go i actually have two typos one is here in position and another one in parameters that's why i said that i really wish there was a better way to type this now if i play the game though my character can walk and if i press shift and try to move forward he'll be running next i'll set up the crouching actions so i'll zoom out a little bit add another blend space 1d and i'll call this one crouching i'll open the editor and here as my initial value i'll again set 0 and this is where i'll have crouch idle and here i'll set this one to three and that is because if you remember when i was testing the velocity values i found out that when i'm pressing the crouch key i get a velocity of around three so in here i'm going to left click and add crouch idle and at the end i'll add the crouch walk and now i want to blend these two actions so we have a blend two node which could work but actually the one that i'm going to try to use is transition and initially transition doesn't have any inputs we have to set the amount of inputs over here in the inspector and i want two inputs and nothing happens so i'll have to save my character close it and open the character tscn file again and now i'll see my inputs so it seems that at least in this version of godot the transition node is not updating right away okay now i want to unlink this from the output and and plug it into the first transition state and i want to plug crouching in the second state i'll zoom in and plug it and then the transition i'll plug into the output and i'll call this transition stand crouch okay now in the script i'm going to go to this line that we just added a minute ago and press ctrl d and that will duplicate the whole line and here instead of idle walk run i'm going to replace that with crouching other than that it's exactly the same thing we want the we want the blend position of crouching to depend on the velocity and now to actually make the character crouch i have to tell it to go to state 1 of this transition and that needs to happen when i press the crouch key so over here on the next line i'm going to type dollar animation tree dot set now we want parameters slash stand crouch slash and the stand crouch has a parameter called current with lowercase and then comma one so by typing one i'm accessing state one of this transition and i also want to tell it to be at state zero when i'm running or walking so i'll i'll copy this whole line and paste it here and just replace zero with one and also in the else statement i'm going to add the same thing and set the current value to one let's play the game and see what happens i can walk and if i press ctrl i go into a crouch state and i can also crouch walk now the crouching happens in an instant and we can fix that by going into the transition node and changing x fade time and i found that a value of 0.2 is nice not too fast and not too slow okay let's go back to the animation tree and let's add the jump animation i'm going to right click choose animation and click over here and choose jump i can name this animation jump as well although i don't think i'll be using it in the script and to mix these states i'm going to right click and the one shot node is actually perfect for this when one shot is turned on it will execute the shot animation once and then it will be turned off automatically again so i'm going to plug stand and crouch into the in node and jump into this shot and one shot into the output and let's name this jump shot or something like that and now to use this in our script i'll go to my if statement for the jump add a new line dollar animation tree dot set parameters jump shot and let's see what kind of parameters it has it has a parameter called active so i'm going to set active lowercase to true and let's give it a try if i press spacebar my character will jump and he'll also play the jump animation cool and now the character can actually walk crouch and jump which is all of the main actions that we intended for him if you remember though there was this special stretch animation that we haven't addressed yet and for that i'll actually need another input so project project settings input map i'll add an action called special i'll add a key q key and to add this action to our tree here i'm going to add another animation set it to the stretch animation and for the mix i'm going to use another one-shot node set my connections i'll call this one stretch shot and plug it into output and now in the script inside the process function somewhere at the end for example i'll add another if statement if input is action pressed special then animation tree dot set parameters straight shot [Music] active true let's give it a try now if i press q my character goes into the default pose and plays this stretching animation so let's go back to the animation tree and for one shot we can edit filters let's click on it and now i want to find and check only the bones of my left arm which is actually the one that is stretching so here i have the left shoulder so i'm going to select the left shoulder left upper arm all the way down to the hand and before i press ok something that i often forget is to enable filtering without enabling this this filtering won't work so check it and press ok and now let's play the game if i press q my character will stretch the hand while he's playing the idle animation and now if i walk i can also press q and the character will walk and stretch then i can jump and stretch and so on so the stretching animation will be mixing with my other animations so with that the character can actually perform everything all the animations that i wanted one little problem that i noticed is the camera behavior when the mouse goes too far to the edge of the screen you may be unable to rotate further so to fix this i'll go to my ready function i still don't have anything on it so i'm going to delete the pass statement and in here i'm going to type input dot set mouse set mouse mode and i'm going to set it to input mouse mode captured and this hides the mouse and also i think it keeps the actual position of the mouse right in the center of the screen at all times so we don't experience the problems that we were experiencing before give it a try and there you go i have no mouse cursor and it is much easier to control my camera and that's basically all i wanted for a simple project you could flesh out your world scene by making copies of this floor maybe adding some cubes maybe add some materials i could also add a directional light that will act as the sun in the scene enable shadows and give it a tiny bit of contact shadow i should have added collision to all of these boxes so select the boxes and make them into a static body cool now i can walk around jump on boxes congratulations for staying until the end of this one hour long video i hope you learned something useful as always big thanks to my patreon supporters please click like and subscribe ring the bell and if you do i'll talk to you next time
Info
Channel: CGDive
Views: 8,284
Rating: undefined out of 5
Keywords:
Id: qwz9aPdVoFg
Channel Id: undefined
Length: 62min 29sec (3749 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.