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...,
Answer by f03n1x · Mar 12, 2019 at 01:44 AM
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: https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
"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!
Your answer
Follow this Question
Related Questions
[SOLVED] Ball rolling around a planet bounces off? 2 Answers
Kinematic forces under Rigidbody Parents 0 Answers
Rigidbody is colliding terrain on isKinematic = false. call although meshes don't touch 0 Answers
how to prevent a kinematic rigidbody from going through static colliders 0 Answers
How to check if kinematic rigidbody is overlapping anything in the scene? 0 Answers