I’m having an extremely annoying problem… and I’m only 5 minutes in to using Unity’s RigidBody physics.
It seems that when an object falls onto a perfectly flat (facing face up, the normal is (0,1,0) ) plane, there seems to be some weird X velocity reaction force added. The end result is that a basic RigidBody cube with a constant force applied in the Z direction only will start moving in seemingly-random way along the X axis.
What I did:
Create a Plane object, scale it up a bit along the Z axis
Create a Cube, position it slightly above one end of the long plane (0,2,0)
Add a RigidBody component to the Cube, chose “Freeze Rotation” (is this part of the problem??)
Add a Constant Force component to the cube, give it 20 for Z and 0 for everything else.
Clicked the Cube to observe its position values
Played the game!
The cube’s X coordinate is 0 as the game starts and remains 0 until it hits the plane, then seems to start heading to the right, and if I make the floor plane sufficiently long it’ll actually fall off the RIGHT side.
Are Unity’s physics really bad? Am I doing something wrong? I’ve worked with game engine physics before (Blender) and I’ve never had weird stuff like this. A basic object falling on perfectly flat plane with force applied on ONE axis SHOULD only go in the direction the force is applied.
I hope that this works… I’ve never exported a unitypackage.
Really, you can replicate this (at least I can) by doing the steps I outlined above. Maybe it’s my computer? I’m still definitely noticing the problem, and it seems quite extreme. The added X velocity ONLY happens on the collision with the (perfectly flat) ground.
Very weird. I’d attach that to a bug report. It seems to be a problem with the Constant Force component; there is some error adding force through script, but it is miniscule compared to this case.
I can notice slight X displacement regardless of whether or not the constant force component is attached. If you watch the X when the object hits the ground, it seems to move very slightly… as if the collision is causing that sort of force.
I noticed this when I WASN’T using a constant force component, and was just applying force through a script.
I think that this is a problem with the Box Collider component. Changing to a Sphere collider seems to mostly eliminate the weirdness.
I’m sad that in the 5 minutes that I messed with Unity RigidBodies I’ve already run into a bug =(. It seems like an extremely large problem too… grr.
It is not really a bug, rather your setup is sensitive to the buildup of numerical errors. What you can do is:
Replace the mesh collider of the plane with a box collider (you should set the y-size to for example 0.1). This reduces the sideways jump at first contact.
Create a new physics material and set the friction to a small value (~0.01). Apply this material to both the plane and the cube.
You should now see much improved accuracy, a quick test gave only 0.03 sideways displacement at the end of the plane. Should you need more precision you have to start decreasing the physics time-step.
I understand that physics/collision detection is, at times, not 100% accurate, and I always expect SOME imprecision, but this seemed to be orders of magnitude worse.
Is the mesh collider just… innaccurate? My levels that I design might need to have them… and I figured that a static object shouldn’t have the huge problems that these examples seem to have. I hate to compare, but Blender’s physics engine (driven by Bullet) doesn’t have this sort of problem at all (though it does have other bugs). Isn’t Unity supposed to be using PhysX?
I can understand small inaccuracies… but this seems to start altering the X position of the object much more quickly than I would expect. I’ll see what I can do to work around it, but it’s a little disheartening.
TwinFox, I thought of both of those things; the box collider changed nothing, and while yes, changinging dynamic friction greatly improved the error, it isn’t a solution, because everything can’t be slippery.
Jessy, you are right on both accounts. I don’t know why I saw a big improvement in accuracy when I initially changed the plane collider to a cube one, but trying it now does not improve the situation.
However, I did some further testing and it seems PhysX is stable only on certain contact point configurations. The simulation is accurate if the cube is rotated 45° to stand on its edge or on its corner (with freeze rotate on). When the cube lies flat on its face or the angle is not exactly 45° the error emerges. Perhaps a bug in calculating the hit normal? Setting friction to zero will remove the sideways movement, so it does play a role in the problem.
I was having similar isues with a sphere collider that’s being thrown in the air (not rolling against a surface or anything) but I decided to keep it as a feature instead of freezing the rotation. It sort of makes sense for an electrical ball to have a somewhat erratic movement: