How to make two rigidbody stop when colllide?

Hello friends,

I have a game scene that has two characters with rigidbody and collision. Both of them is moved by keyboard with using rigidbody.movePosition. Now I want to make both of them stop when they collide with each other but don’t know how…

I tried to set one of them to be kinematic, but it only works in one side, when A hit B it works, but when B hit A it won’t work, also clearly that i can;t set them both to kinematic…

Is there a better way to solve this problem ?..

Many thank in advance…,

Have you tried something like setting their velocity to 0? so something along the lines of

RigidBody rigidBody = collider.GetComponent<Rigidbody>();

if(rigidBody)
{
    GetComponent<RigidBody>().velocity = Vector3.zero;
    rigidBody.velocity = Vector3.zero;
}

Hmm seems like Move Position acts a little differently reading the information from here: Unity - Scripting API: Rigidbody.MovePosition

“If the rigidbody has isKinematic set false then it works differently. It works like transform.position=newPosition and teleports the object (rather than a smooth transition).”

It seems to still move when you set it to isKinematic, so you may have to do something like inform the component that contains the keyboard conditions and tell it to stop checking for if a key is held down or something similar to that.

I haven’t tested any of this out, but I’ll have a go later today if it doesn’t work out for you, good luck!