Mecanim to Ragdoll in Unity 3d

Sharing is caring:

Setting Up Our Ragdoll

In this three part Unity 3d tutorial we’ll be showing you how to setup a character as a ragdoll, apply animations using Mecanim, and turn him into a ragdoll once we shoot a projectile at him. This is part one of the series that will cover how to properly setup a ragdoll with our rigged character. For this video we’ll be using a free asset off the Unity 3d asset store that contains the rigged character and animations needed.

Applying Mecanim Animations

In this second video of our Mecanim to Ragdoll tutorial series we’ll be adding animations to our character using Mecanim. This video will not be going in depth with how to use Mecanim but will teach you the very basics of setting up a character to use animations with Mecanim. The packaged (link below) asset comes with a plethora of animations that we can use. For this video we’ll be going over a few of them to include an idle animation and a running/jumping type of animation. We’ll show you the basics of trimming an animation clip and how to seamlessly loop an animation clip to play over and over again. We’ll briefly go over the animator controller window and some of the settings that we’ll be changing.

Activating Ragdoll on Collision

In this final video we’ll wrap things up by showing you how to apply scripts to our character to turn him into a ragdoll once we shoot him with a cannonball. We’ll be using three different scripts; one for the instantiation, one for the collision to be registered, and one for the ragdoll “activation”. You’ll need to create a prefab of the cannonball that has a rigidbody attached to it. This will register a collision once it comes in contact with any of the colliders attached to our rigidbody bones. The cannonball will have a script that contains a “OnCollisionEnter” script that will use a “Send Message” to call a function from our “ragdollCharacter” script. The character will have the ragdollCharacter script that will “collect” all the rigidbody bones and disable the “isKinematic” feature. We’ll also be disabling the Animator Controller on our character so that the animations will stop playing and he will turn into a ragdoll. Scripts and link to Asset can be found below:

Assets Used

Raw Mocap data for Mecanim

Javascript Files

cannonballHit.js (apply to the cannonball prefab)


#pragma strict

function OnCollisionEnter (col : Collision) {

if(col.gameObject.tag == "ragdollBones") {

gameObject.Find("Ragdoll").SendMessage("killRagdoll");

}
Destroy(gameObject, 3);

}

CannonShoot.js (apply to empty gameObject to instantiate cannonball prefab)


#pragma strict

var cannonball : GameObject; //Prefab of your cannonball or projectile
var rateOfFire : float = 0.5; //Determines the rate of fire
internal var fireDelay : float; //Determines delay between when you can shoot
var speed : float = 20; //Determines the speed at which you can shoot

function Update () {

if (Input.GetButton("Fire1") && Time.time > fireDelay) {  //Ensures that player has pressed the "Fire1" button and all other parameters have been met before firing

fireDelay = Time.time + rateOfFire;
var clone : GameObject = Instantiate (cannonball, transform.position, transform.rotation); //Clones prefab "cannonball"
clone.rigidbody.velocity = transform.TransformDirection(Vector3 (0,0,speed));//Shoots "cannonball" in the Z-axis based off "speed" variable
Physics.IgnoreCollision(clone.collider, transform.root.collider);

}

}

RagdollController.js (apply to our character)


#pragma strict

var boneRig : Component[];//used to store the rigidbody bones of our ragdoll

function Start () {

boneRig = gameObject.GetComponentsInChildren (Rigidbody); //grabs the rigidbodies of our bones to start

}

function killRagdoll () {

for (var ragdoll : Rigidbody in boneRig) {

ragdoll.isKinematic = false; //disables the animated ragdoll and turns it into just a regular ragdoll with no movement
}
GetComponent(Animator).enabled=false; //disables the mecanim animator so our character no longer has any animations playing
}

 

Comments

  1. Nice tutorial, thanks!

  2. any chance of the source?

    • Jonathan Gonzalez says:

      You mean the project files? I cannot provide those as the packages needed are not mine so I’m not able to distribute them. If you’re stuck on something let me know and I’ll try to help.

      • No problem. My issue is my character us is running quite fast. It’s actually using the forward run from the mocap data. When I turn him to ragdoll the model glitches his ass comes right out. Parts of the chest elongate etc. It’s fine if I kill him while walking. I’ve been stuck on this for weeks

Leave a Reply