determining speed at a collision

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. :frowning:

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? :slight_smile:

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

I’ve had similar problems and solved them by maintaining the average velocity over the past n FixedUpdates and using that.

But if somebody’s got a tidier method, I’d be glad to hear about it.

I may give up on CharacterControllers entirely and run everything as rigidbodies with isKinematic set true and a collider set as a trigger. Would have to duplicate the CharacterController.Move logic but I dont think that would be so bad.

Thank you for the idea, it helped me!
Well, my problem was a bit different, because it turned out that the whole event doesnt want to start, so i tried the other way: the car’s collider noticed the collision with the controller, so i added this to the car’s script:

function OnCollisionEnter(collisionInfo : Collision) {
	var other:PlayerTPSController=collisionInfo.gameObject.GetComponent(PlayerTPSController);
	if (other!=null)
	{
		//Debug.Log(rigidbody.velocity.magnitude+", "+prevSpeed);
		other.manualCollider(rigidbody,myTransform.TransformDirection(prevSpeed),rigidbody.mass);
	}
}

(prevspeed was used before for the car as a relative vector, thats why i transform direction it. and prevspeed shows the right velocity (its from a previous update event), while rigidbody.velocity.magnitude shows the wrong, after-collision speed indeed)

in the colliders script:

function manualCollider(carRigidbody:Rigidbody,carSpeed:Vector3,carMass:float)
{
		if (!killmeOnce) return;
			killmeOnce=false;
		Destroy(gameObject);
		var dead: Transform=Instantiate(rigidBody, transform.position, transform.rotation);
		dead.GetComponent(Ragdoll).hitby=carRigidbody;
		dead.GetComponent(Ragdoll).hitWithVel=carSpeed;
		CopyTransformsRecurse(myTransform, dead);
		return;
}

sofar it looks like it works ok. at least i cant recreate it with the previous bug-recreation method.

I was beginning to think on Physics.OverlapSphere, or something other Physics thing, to make a manual collider. :stuck_out_tongue: But this works better.

Just to note, the Collision object passed to OnCollisionEnter has a field for the relativeVelocity of the two objects at the point of collision.

4 Likes