- Home /
How can I make character become a ragdoll with isKinematic?
I've been trying to create a script that makes my Player become a ragdoll when you press the space bar. The Player has a ragdoll with each limb having isKinematic to true. Basically, I want to have it so that if you press the space bar, isKinematic for each limb will be set to false so that they become a ragdoll and the script player script called 'ThirdPersonController' be deactivated. Also, I was trying to have it so that after 5 seconds, the ragdoll resets to the player by activating the 'ThirdPersonController' script and setting isKinematic for the limbs to false. This isn't working as the Limbs remain with isKinematic set to true, but the boolean itself is being activated, so could somebody please help me with my script? I'm quite new to coding.
Here is my script:
var Limbs : GameObject[];
var IsRagdoll : boolean = false;
function Update(){
if(Input.GetKeyDown("space")){
IsRagdoll = true;
}
}
function RagdollActive(){
if(IsRagdoll == true);
{
Limbs.isKinematic = false;
GetComponent(ThirdPersonController).enabled = false;
}
if(IsRagdoll == false);
{
Limbs.isKinematic = true;
}
}
Answer by AlucardJay · Apr 10, 2014 at 06:24 PM
Some points :
isKinematic is a variable of the Rigidbody component, not the GameObject component. (this should not even compile)
Limbs is an array, so you must loop through each element in the array and modify each
the function RagDollActive is never called
you should not have a semicolon after an if statement (at line 11)
normally variable names are in camelCase, class and components are in UpperCase
You can call a function after a delay period using the Invoke command.
So here is an (untested) script. Please read through the comments to help understand what is happening :
#pragma strict
var limbs : Rigidbody[]; // an array of Rigidbody components (drag and drop in Inspector)
var isRagdoll : boolean = false;
function Update(){
if(Input.GetKeyDown("space"))
{
if(!isRagdoll) // this is the same as writing if (isRagdoll==false)
{
RagdollActive();
}
}
}
function RagdollActive()
{
isRagdoll = true; // set ragdoll to true
// now if space is hit while isRagdoll == true, this function won't be called
for(var limb:Rigidbody in limbs) // for each limb in the array limbs
{
limb.isKinematic = false; // set the variable of the Rigidbody component
}
GetComponent(ThirdPersonController).enabled = false;
// invoke a function after 5 seconds to reactivate object
Invoke( "RagdollInactive", 5.0 );
}
function RagdollInactive()
{
for(var limb:Rigidbody in limbs) // for each limb in the array limbs
{
limb.isKinematic = true; // set the variable of the Rigidbody component
}
isRagdoll = false; // set ragdoll back to false
GetComponent(ThirdPersonController).enabled = true;
}
Thank you so much, that was really bugging me; sorry for asking for help again but is there a way to have the character controller follow the ragdoll because when the player becomes a ragdoll, the character controller will remain where it was before and when the player returns to normal, they sort of warp right back to the character controller, if you see what I mean.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problems with ragdolls and no gravity 0 Answers
Disable ragdoll without changing Kinematic boolean? 2 Answers
Tracking Force Added to Kinematic Rigidbody 2 Answers
How to make foot don't cross the ball? 0 Answers