- Home /
adding force to instantiated ragdoll
Hello Unity-Community,
I've managed to create a ragdoll via ragdoll-wizard and it works so far. Now I'm trying to add force to the instantiated ragdoll because the enemy is shot but it only collapses on the spot.
Here is my script which is attached to the enemy:
var deadReplacement : Transform;
function Update() { if(HitCollisionScript.NPC_dead) { replaceWithRagdoll(); } }
function replaceWithRagdoll() { // Destroy ourselves Destroy(gameObject);
// Replace ourselves with the dead body
if (deadReplacement)
{
var deadBody = Instantiate(deadReplacement, transform.position, transform.rotation);
deadBody.rigidbody.AddForce(transform.forward * 1000);
}
}
I would really appreciate a hint why it is not working
greetings Pengu
Answer by spinaljack · Jul 15, 2010 at 12:15 PM
You've set the instantiate pointer to a transform i.e. a set of coordinates and scales.
What you need to do is set the instantiate pointer to a GameObject prefab.
var deadReplacement : GameObject;
Then you drag your ragdoll into an empty prefab to save it.
Then you drag the prefab onto the deadReplacment slot in the inspector.
Make sure that the root object of the ragdoll prefab has a rigid body as you're trying to access it.
Answer by The-Arc-Games · Nov 04, 2011 at 07:30 AM
I'm sorry to reply just now but we made a tool for the asset store that shows how to do this even in the awesome and feature full free version (forum thread and links here), that I'd suggest you install to get a number of leads on how to properly ragdoll and interact with game models and physics.
Besides, you basically need to 'intercept' the collider or trigger event, and add a relative force in the point of contact, like this
rigidbody.AddForceAtPosition(myforce,myimpactpoint, myforcemode);
which basically adds a force to the rigidbody into a point, causing torque and translation. Note in fact that the forcemode is very important, since it determines the kind of impulse that the rigidbody will receive.
Im also having issues with this. I have a ragdoll that has all it's rigidbodies set to is$$anonymous$$inematic. On collision enter I turn all the rigidbodies to is$$anonymous$$inematic==false. But the impact force is lost during the computation causing the ragdoll just to drop to the ground limply.
You are suggesting that I (if I am to understand this correctly.) store the impact point that triggers the event and calculate force on my own and reapply a new force that simulates the impact using
rigidbody.AddForceAtPosition(myforce,myimpactpoint, myforcemode);
That may just work. Good idea.
Answer by Pengu · Jul 15, 2010 at 08:45 PM
weird, setting the instantiate pointer to a GameObject prefab didn't make any difference for me but
Make sure that the root object of the ragdoll prefab has a rigid body as you're trying to access it.
ringed the bells!
my mistake was that I didn't access the ragdoll root bone directly (or which ever part I wanted to push)
instead of
FAIL:
deadBody.rigidbody.AddForce(transform.forward * 1000);
I did
OK:
gameObject.Find("ragdoll_pelvis").rigidbody.AddForce(-transform.forward * 1000)
"ragdoll_pelvis" = name of my root object / -transform = to push him backwards
and it worked!
thanks for the help spinaljack :)
Hi, I used your technique, and it work great, so thanks for that. But I get a weird clipping bug. When the camera go pass the position where the Ragdoll was created and where I destroy the original character, the Ragdoll get clipped and disapear from screen. If I move back a bit, the Ragdoll reappear. Any Idea what could cause this? Thanks
Ok, I found the solution. The Update when offscreen in the $$anonymous$$esh Renderer was not set to true. I set it, and it work now.
Answer by OoglyWoogly · Apr 18, 2011 at 06:02 PM
You should use AddRelativeForce
so that the ragdoll moves relative to the direction it has been hit from. The syntax is the same as add force. Its just that if it is shot from the left it will fall to the right, rather than always falling in one direction.
Your answer
Follow this Question
Related Questions
Ragdolling not properly problem. 1 Answer
Applying force to a rigidbody 2 Answers
Disabling Kinematic on hit 2 Answers
Projectile not moving properly in top down 2d shooter 2 Answers
How to calculate force from explosion on a rigidbody 1 Answer