0
\$\begingroup\$

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.

Basic view of the game in development state. Avatar is on the left

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.

The new platform is in white, and test cube in red. No bug occurs when the avatar goes through the platform

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.

Collision prior to the force application. Then, the avatar will drift towards the bottom of the screen, until it hits the floor. Same happens if it hits another side, it just get push until it it something. The player can still control the avatar, but the controls are hindered by the force

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)

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Tartle Wizard Dec 14 '16 at 18:00
  • \$\begingroup\$ 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) \$\endgroup\$ – Nyakouai Dec 15 '16 at 18:40
1
\$\begingroup\$

I think you are seeing physics in action. The flying character is not ignoring the collision with the cube -- correct? So when you touch the cube there is an equal and opposite force applied to the cube and the player (a law in physics), that is why the character moves down. Without friction or another object to collide into the character the motion in that downward direction will not stop. This is another law: an object either remains at rest or continues to move at a constant velocity unless acted upon

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.

\$\endgroup\$
  • \$\begingroup\$ Yes, I do collide with the red cube. I had more or less figured that it had to do with physics from the collision, I just didn't know how to get rid of the after effect. Alas, I need to do precise movements (i.e. stop when no key is down) so forces are a tad complicated. Thanks for the info, though, I tried to set the velocity to 0 and it kind of solved the problem. \$\endgroup\$ – Nyakouai Dec 15 '16 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.