# Unity - Permanent force when a rigibody without gravity hit a collider

I'm actually working on a 2D game, and stumbled upon something rather strange in Unity. The issue occurs pretty specifically, so the description will be long:

In the scene, I have a flying object, controlled by the player. It has a rigidbody set as follows: Mass=1, Drag=0, Angular Drag=0.05, Gravity=false, Kinematic=false. So far, so good, the script worked flawlessly, and collide as expected in other physical objects.

Our client asked us to create platforms upon which the enemies could walk, but the player would fly through. A simple collider with IgnorePhysics(GameObject with Player tag) did the trick. The script still worked, no issue until then.

I added a cube on the platform, to test if it would fall through or not. The cube effectively collide with the platform, just as usual, and rest upon it.

But now, as soon as the player hit the cube, it get pushed back in the opposite direction until it hit another surface. It happens like when a non-gravity object is pushed by something: a force is applied and continue forever without friction. (But it does not bounce the same way on other objects. I can only suppose it comes from the force inputed to the objects during the collision, but I can't know for sure)

Example: if I hit the underside of the cube, my avatar will drift towards the ground until I hit it, no matter its speed in other direction. If I hit the upperside, it'll just fly out of the screen, towards the y-infinite.

The script is a classic gameobject.transform.position = Vector3.MoveTowards, so it input no forces whatsoever. Thus, I can't counter the inverse force.

The thing bothering me is that the avatar never had this kind of drift before, even though it collided other objects.

My question is: How can I negate the new force input from the collision for the avatar? (optionnaly: why does it occurs only in this case? I'm puzzled by this case)

• if possible it might help to include screenshot. Are you using ray casting on the character to detect the ground and change between flying/walking? Commented Dec 14, 2016 at 18:00
• Added some, to make the question clearer. Just in case, the avatar only move in the XY plane and in the screen boundaries. It is not subject to gravity, and collide with every physical object, unless stated otherwise (the platform ignore collisions with the avatar). Never had this "bug" occurs before, and I can't really figure out why it does now (only occurs when collisionnng with the red cube) Commented Dec 15, 2016 at 18:40

Your controller code gameobject.transform.position = Vector3.MoveTowards is moving the player directly through the use of the Transform without adding in new forces, but the previously mentioned force changed the rigidBody velocity thus the physics engine is still moving it too. To remove the force, do this at some point after the collision rb.velocity = Vector3.ZERO;
As a further note, you usually don't want to mix RigidBody on a non kinematic object and also have a script or animation that moves the object via the Transform. This leads to multiple things competing on how the object moves and you will get weird results.