- Home /
Stop movement of rigid bodies 2D after collision
I'm having two rigid bodies in my game. I'm using rigidbody2D.addForce to move these rigid bodies. When they collide with a non-rigid body collider/gameobject they don't move after collision. But, when they collide with each other(colliding with an rigid body) these objects move after collision, depending upon how much force is applied. I cannot destroy rigidbodies after collision because I'm using those for their movements.
Below is how I'm moving my objects :-
if (Input.GetKeyDown (KeyCode.RightArrow))
{
//transform.Translate(0.5f, 0, 0);
rigidbody2D.AddForce(Vector2.right * swipeSpeed);
}
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
//transform.Translate(-0.2f, 0, 0);
rigidbody2D.AddForce(-Vector2.right * swipeSpeed);
}
if (Input.GetKeyDown (KeyCode.UpArrow)) {
//transform.Translate(0, 0.2f, 0);
rigidbody2D.AddForce(Vector2.up * swipeSpeed);
}
if (Input.GetKeyDown (KeyCode.DownArrow)) {
//transform.Translate(0, -0.2f, 0);
rigidbody2D.AddForce(-Vector2.up * swipeSpeed);
}
Answer by fafase · Oct 27, 2014 at 06:59 PM
If you want to totally stop movement, you can set isKinematic to true in OnCollisionEnter2D
If you want to avoid bounciness, create a new PhysicsMaterial2D and set bounciness to 0.
If both are not suiting you then by code you can set the velocity to Vector2.zero in the OnCollisionEnter2D
Thanks for the answer. I had tried all these options earlier as well but they were not too friendly with me. I may have missed this one, but in my game I also want the objects to move while they are in collision. These movements would be when input is from keyboard & not because of the rigidBody repulsion movements.
Answer by hammad-zahid · Dec 08, 2015 at 12:02 PM
Well, Its simple: In Constraints (Rigidbody), Check Freeze Rotation z or see in the inspector which axis is causing your objects Rotation... Its solution unity5.1.2
Your answer
Follow this Question
Related Questions
Rigidbody2D.AddForce works only once? 1 Answer
Character on platform move slowly 1 Answer
Do rigid bodies with force add force to other objects? 0 Answers
rb.AddForce, rb.MovePosition, transform.localPosition: no solution working for 3D motion+collision 0 Answers
Why doesn't Rigidbody.velocity.x = 0 in a collision? 1 Answer