Instantiated Ragdoll bounces back for 1 frame

Hi All!

If been polishing up on my ragdoll skills, but there is one little last problem…
Upon Instantiating the ragdoll, I give it the velocity of the gameobject that spawned it. However there seems to be this little jitter, that influences the rest of the trajectory of the ragdoll. Upon examining slowed down video footage I noticed that jitter is happening the first frame the ragdoll is spawned. It bounces back just a tiny bit, before continuing in the correct direction…

You can see the footage here: http://www.youtube.com/watch?v=LR3zxVNv1yA

This is the relevant code from the script attachted to the original rigidbody:

function FixedUpdate()
{
lastVel = (rigidbody.velocity * -1);
lastAngVel = (rigidbody.angularVelocity * -1);
}

function OnCollisionEnter(collision : Collision)
{
    if(collision.gameObject.tag == "Player")
    {
    ragdoll = Instantiate(ragdoll, transform.position, transform.rotation);
    colliders = ragdoll.GetComponentsInChildren(Collider);
        for (var col : Collider in colliders)
    	{
	Physics.IgnoreCollision(col, collider);
    	}
   	rigidbodies = ragdoll.GetComponentsInChildren(Rigidbody);

	for (var child : Rigidbody in rigidbodies)
		{
		child.velocity = lastVel;
        	child.angularVelocity = lastAngVel;
		}	
	Destroy(gameObject);
    }
}

Obviously, there are a number of different reasons that can cause this problem, but after a few hours I haven’t been able to determine what is causing this, and why.

I found one strange thing though: If i don’t multiply the original rigidbody’s velocity with -1, it bounces the ragdoll backwards / off the player, instead of over / through him. For the ragdoll i used the same model, with the same anchor point etc as for the animated model, so it’s not the axis’s. Although this is odd, I dont see how turning the velocity value into negatives should push the ragdoll back 1 frame, and then not…

Anybody any idea on what’s the cause of this, and how to fix it? Can it perhaps be the joints? Any help will be greatly apriciated!

Thanks in advance!!
Thomas

I tried an alternative: I created an auxiliary trigger volume a little bigger than the actual object’s collider, and changed the script to do the ragdoll replacement in OnTriggerEnter instead of OnCollisionEnter - this way, the runner guy will first encounter the trigger and be replaced by the ragdoll, which will actually collide with the obstacle. It worked fine for me - except by the fact that there was two trigger events, thus I had to create a boolean variable replaced that eliminated the second event.

var ragdollPrefab: Transform;
private var replaced = false; // avoids second trigger

function OnTriggerEnter(col : Collider){
    if (!replaced && col.tag == "Player"){
        replaced = true;
        var ragdoll = Instantiate(ragdollPrefab, transform.position, transform.rotation);
        var rigidbodies = ragdoll.GetComponentsInChildren(Rigidbody);
        for (var child : Rigidbody in rigidbodies) {
            child.velocity = rigidbody.velocity;
            child.angularVelocity = rigidbody.angularVelocity;
        }
        Destroy(gameObject);
    }
}

Try this code in your project: create a box trigger and place it a little before the obstacle, and attach this script to the runner guy.