Triggering Ragdoll Physics (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at how we can implement ragdoll physics in unity first of all we'll look at how to configure a ragdoll for a character and then we'll look at how to activate the ragdoll when the character's killed we'll also look at how we can target specific body parts and have that affect the ragdoll okay let's start by creating a new 3d project in unity hub next we'll go to the unity asset store at assetstore.unity.com we're going to look for a free asset to use for the scene environment we'll search for industrial set v3 we'll add this one to our assets and open it in unity we'll download and import the asset into the project then we'll navigate to the new asset in the assets panel we'll double click the map v1 scene to open it there's no camera currently in this scene so we'll click the plus button in the hierarchy and add a camera we'll set the position of the camera to 15 on the x-axis 1.5 on the y-axis and minus 1 on the z-axis then we'll set the rotation to 5 on the x-axis and minus 130 on the y-axis next we want to add a character to the scene for this we'll head over to mixmo.com this is a free resource that just requires you to create an account to get access to various characters and animations we'll go to the characters tab and we'll select this character called romero we'll click download we'll select fbx for unity and then click download we'll create a new folder in the assets folder of the project we'll call this models and save the file to this folder back in unity we'll go to the models directory in the assets panel and then we'll drag the character onto the hierarchy we'll rename this zombie let's move the zombie so we can get a better view we'll set the position to 12 on the x-axis minus 0.1 on the y-axis and minus 3 on the z-axis we'll also set the rotation to 45 on the y-axis you'll notice that the textures are missing at the moment this is because they're embedded in the fbx file we can extract them by clicking on the model in the assets panel then on the materials tab and then on extract textures we'll create a textures folder to extract to we'll get a pop-up regarding an issue with a normal map which we can just click on fix now to resolve next we're going to look at how to set up a ragdoll for the zombie we can use the ragdoll wizard in unity to help us with this to open it we'll go to the main menu and select game object 3d object ragdoll we need to drag in the correct bones of the zombie and then this wizard will create all the required physics components for us we'll expand the zombie character in the hierarchy then we'll drag the hip bone into the pelvis slot left up leg into left hips left leg into left knee and left foot into left foot then we'll do the same for the right leg we'll drag the left arm into the left arm slot and left forearm into left elbow then we'll do the same for the right arm for middle spine we'll select spine one and we'll drag the head into the head slot finally we'll set the total mass of the character to 70 kilograms and click create let's have a look at what this has done we'll switch to the scene view and double-click the zombie to zoom in on it now we can see that it's created colliders for each part of the character if we click on the left leg for example we can see that we now have a capsule collider a rigid body and a character joint this is everything needed to have the body parts be controlled by the physics system if we wanted to we could fine-tune this and make the colliders better fit our model we'll just leave it as it is for now and press play the zombie now collapses in a heap when the game starts the next step is to be able to control when the ragdoll effect is activated let's stop the game and look at how to do this we'll navigate to the root folder in the assets panel then we'll click the plus button and create a new folder we'll call this scripts we'll double click this new folder then we'll click the plus button and add a new script we'll call this zombie we'll drag this script onto the zombie in the hierarchy to assign it then we'll double-click the script to open it in the editor what we want to do is disable the rigid bodies of the ragdoll when the game starts we'll create a private array field to store the rigid bodies then we'll replace the start method with the awake method in here we'll populate the array by getting all the rigid body components from the child objects next we'll create a private method called disable ragdoll in here we'll loop through all the rigid bodies we'll then set is kinematic to true on each one this will stop physics affecting the rigid body we'll call this method from the awake method then we'll create another private method called enable ragdoll we'll loop through the rigid bodies again this time we'll set iskinematic to false now in the update method we'll check if the spacebar has been pressed if it has we'll enable the rag doll let's save the script and switch back to unity to try this out now when we press the spacebar the zombie collapses let's stop the game and take this a step further by making the zombie walk towards the camera we'll head back over to miximo and go to the animation tab we'll search for zombie walk we'll select this one then we'll click download we'll select without skin to just download the animation without the model then we'll click download we'll create a new folder in the assets folder of the project we'll call this animations and save the file to this folder back in unity we're going to switch to using the humanoid animation type we'll navigate to the models folder in the assets panel and select the zombie model then we'll select the rig tab in the inspector and change the animation type to humanoid we'll click apply to save the changes then we'll go to the animations folder and select the walking animation we just downloaded we'll change this to humanoid as well for the avatar definition we'll select copy from other avatar and select the avatar that's been created for the model we'll click apply to save the changes if any of this seems unfamiliar then take a look at our video on animation retargeting where this is covered in more detail next we'll go to the animation tab we'll tick loop time and loop pose to have the animation loop forever we're going to use root motion to drive the zombie forwards we don't want the animation to influence the rotation or the y position though to stop this happening we'll bake these into the pose we'll also select original for the based upon values to use the original values from the animation if you want more detail on this then take a look at our video explaining root motion we'll click apply to save these changes then we'll expand the animation in the assets panel and drag the clip onto the zombie in the hierarchy this has created an animator controller and assigned the walking animation as the start state in addition to the animation we'll also add a character controller this will keep the zombie on the floor and stop it walking through walls to do this we'll click add component and search for character controller we'll update some of the settings to make the character controller better fit the zombie we'll set the min move distance to 0 the center y value to 0.98 the radius to 0.3 and the height to 1.8 next we'll go back to the script to have the zombie rotate towards the camera we only want the zombie to rotate towards the camera when it's walking to help us do this we'll set up a basic state machine we'll create a private enum called zombie state in here we'll add two states one for walking and one for ragdoll then we'll create a private field for the current state and set it to walking next we'll create a method for the behavior we want to occur in each state we'll create one called walking behavior and another called ragdoll behaviour we'll cut and paste the existing logic from the update method into the walking behavior method then in the update method we'll create a switch statement on the current state when the state is walking we'll call the walking behavior and when it's ragdoll we'll call the ragdoll behaviour we're not going to do anything specific in the ragdoll behavior in this video but it will come in useful later down the line now let's look at the changes we need to make in the walking behavior first of all we'll change to the ragdoll state after the ragdoll has been enabled then we want to rotate towards the camera to do this we'll add a private field for the camera we'll make this serializable so that it can be set in the inspector then in the walking behavior method we can get the direction from the zombie to the camera we'll do this by subtracting the zombie position from the camera position we only care about the xz direction so we'll clear the y component then we need to normalize the direction vector to set the magnitude to 1. next we need to get the desired destination rotation we'll store this in a quaternion variable which is a type specifically for storing rotations we'll then use the quaternion look rotation method to create a rotation looking in the desired direction we'll set the forward direction to the desired direction and we'll set the up direction to the y axis using vector 3 up now we'll change the rotation of the zombie we'll use the quaternion rotate towards method to rotate from our current rotation towards the desired rotation we'll have the zombie rotate quite slowly at a rate of 20 degrees per second we also need to remember to multiply by time dot delta time let's save the script and switch back to unity we'll drag the camera into the camera slot on the inspector then we'll press play to try it out now the zombie walks towards the camera but when we press space nothing happens that's because the animation is still driving the position of the bones to fix this we need to disable the animator when the ragdoll is triggered let's stop the game and switch back to the script we'll create a private field for the animator we'll also create one for the character controller as we'll need to turn this off as well in the awake method we'll get these two components then in the enable ragdoll method we'll disable both of them for completeness we'll also enable them both in the disable ragdoll method let's save the script and try this out now when we press space the ragdoll is triggered correctly the final thing we're going to look at is how we can shoot specific body parts of the zombie by targeting them with the mouse let's stop the game and navigate to the scripts folder in the assets panel we'll click the plus button and add a new script we'll call this shoot we'll drag this script onto the camera to assign it then we'll double click the script to open it in the editor what we're going to do is apply a force to the body part under the mouse cursor when the left mouse button's released let's start by creating a serializable float field for the maximum force we're going to have the force vary depending on how long the left mouse button is held down so we'll create another serializable float field for the time to reach maximum force we'll also need a private float field to store the time that the mouse button was pressed to determine what the mouse cursor is pointing at we'll also need a field for the camera we'll replace the start method with the awake method in here we'll get a reference to the camera component then in the update method we'll check if the left mouse button is down if it is we'll set the time that the mouse button was down then we'll check if the left mouse button has been released if it has we want to check if any part of the zombie is under the mouse cursor to do this we'll create a ray using the camera screen point array method passing in the current mouse position you can think of a ray as a line that's emitted from a source in a particular direction in this case the source is the camera and the direction is determined by the position of the mouse cursor we can now use the physics raycast method to see if the ray has hit anything this will check if the ray intersects with anything and also populate the raycast hit object with information about the collision if there has been a collision we need to check whether it's with a zombie we'll try to get the zombie component from the collider that's been hit we'll check for the component in any of its parent components as it may be one of the child body parts that we've hit if the zombie component is not null then we know we've hit a zombie if this is the case then we want to trigger the rag doll to allow us to do this we'll switch to the zombie script and add a new public method called trigger ragdoll we'll add a vector3 parameter for the force we want to apply to the ragdoll and we'll add a parameter for the impact point that we want to apply the force to we'll leave this as an empty method for now and switch back to the shoot script to determine how much force to apply we need to work out how long the mouse button was held down for to do this we'll subtract the time that the mouse was held down from the total amount of time that has elapsed in the game we can now use this to calculate the percentage of the maximum force by dividing it by the maximum force time to determine the force magnitude we'll use lerp we'll pass in a minimum value of 1 the maximum force and the force percentage as the interpolation value if you'd like more information on how lerp works then take a look at our dedicated video on this the next thing we're going to do is calculate the direction for the force for this we'll use the direction of the zombie from the camera to calculate this we'll subtract the camera position from the zombie position we're going to modify this direction to give it a value of 1 on the y direction this will give some upward direction to the force lifting the zombie off the ground slightly then we'll normalize this vector to ensure the direction has a magnitude of 1. next we'll calculate the force by combining the force magnitude and direction now we have everything we need to call the new method on the zombie to trigger the rag doll we'll pass through the force we'll also pass through the point the ray hit the zombie let's switch over to the zombie script to fill in this method the first thing we'll do in here is enable the rag doll next we want to apply the force to the body part that's under the mouse cursor we can do this by finding the rigid body that's closest to the hit point to help with this we're going to add the using statement for link link allows us to easily query collections such as our collection of rigid bodies we'll order the rigid bodies by the distance from the hit point now we have the rigid bodies in order of distance we can just take the first one then we'll add a force to this rigid body using add force at position we'll supply the force and the hit point we'll choose the impulse force mode to apply the force instantly finally we'll set the current state to ragdoll and we'll remove the previous logic we had to trigger the rag doll let's save the script and switch back to unity we'll select the camera in the hierarchy then we'll set the maximum force to 1000 and the maximum force time to 2 seconds let's press play to try this out now we can click on the zombie and it will trigger the rag doll and apply a force to the relevant body part we can even shoot the zombie while it's on the floor if we hold the mouse button down it will apply a greater force you'll notice that when we do this the limbs of the zombie get stretched all over the place though let's stop the game to fix this we'll go to the hierarchy and find the character joints then we'll tick the enable projection checkbox this will help improve the stability of the rag doll we'll do this for all the character joints let's try this out now it looks much better the final thing to do is add some more zombies let's stop the game to do this we'll select the zombie in the hierarchy and press control and d to duplicate it a few times then we'll reposition the duplicates around the scene let's try this out now we have a hoard of zombies to shoot the next evolution of this would be to look at how to have the zombies stand back up again after a period of time if you'd like to see a video explaining how to do this then let us know in the comments if we get enough interest then we'll definitely cover it in a future video okay that covers everything for this video a big thank you to all our patrons we really appreciate you helping to support the channel if you'd like to help and also get access to the source code you can find details in the description please leave any questions or feedback in the comments and subscribe and click the bell icon so you don't miss the next one thanks guys
Info
Channel: Ketra Games
Views: 23,784
Rating: undefined out of 5
Keywords: Ragdoll Unity, Ragdoll Physics Unity, Trigger Ragdoll Unity, Target Body Parts Unity, Turn on Ragdoll Unity, Triggering Ragdoll Unity, Turning on Ragdoll Unity, Toggle Ragdoll Unity, Ragdoll Unity 3d, Unity, Unity Tutorial, unity3d, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Tutorial, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: KuMe6Iz8pFI
Channel Id: undefined
Length: 24min 31sec (1471 seconds)
Published: Thu Mar 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.