Hi!
I made a script that transforms a character controller controlled avatar to a ragdoll. But i want it to do that only if the object that is hitting the ragdoll is heavy and/or fast.
My problem is, that i noticed that the velocity of the object hitting my character controller, is sometimes already decreased a bit. My car hits the character controller with 6.2, but as soon as it does that, its speed is decreased, and when the controller notices the collision and checks the car’s speed, it is already very low, like: 0.32. Most of the time it works, but sometimes not, and if not, its very annoying. My average fps is 200+, 60+ if there is a lot of particle on the screen.
here is the script:
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
if (body == null || body.isKinematic)
return;
//Debug.Log(body.velocity.magnitude+body.gameObject.name+body.mass);
if (body.velocity.magnitude*Mathf.Clamp01(body.mass/myMass)>2){
if (!killmeOnce) return;
killmeOnce=false;
Destroy(gameObject);
var dead: Transform=Instantiate(rigidBody, transform.position, transform.rotation);
dead.GetComponent(Ragdoll).hitby=body;
dead.GetComponent(Ragdoll).hitWithVel=body.velocity;
CopyTransformsRecurse(myTransform, dead);
return;
}
}
I made an alternative script because of a less annoying issue: if the controller collides with a ragdoll’s bodypart for example (like, standing on a dead body), then it only checks that bodypart’s weight and speed, and not the car’s weight and speed. I noticed that the controller inherited the OnCollisionEnter, so i made this script:
function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
var body : Rigidbody = contact.otherCollider.transform.root.rigidbody;
if (body == null || body.isKinematic)
return;
Debug.Log(body.velocity.magnitude+body.gameObject.name+body.mass);
if (body.velocity.magnitude*Mathf.Clamp01(body.mass/myMass)>2){
if (!killmeOnce) return;
killmeOnce=false;
Destroy(gameObject);
var dead: Transform=Instantiate(rigidBody, transform.position, transform.rotation);
dead.GetComponent(Ragdoll).hitby=body;
dead.GetComponent(Ragdoll).hitWithVel=body.velocity;
CopyTransformsRecurse(myTransform, dead);
return;
}
}
}
this is even worse: it works very rarely. ![]()
Is there a way to quickly determine the other object’s speed, before the collision? Or it is a bad idea using charactercontroller if i want it to die/collapse? ![]()
Edit: i noticed that sometimes the colliderhit event doesnt recognise my car hitting the collider (no debug message (which is commented in my post, but not in the script)). Maybe it is the floor that prevents the event to notice the car? :S