Unity 5 Physics Update – Will it Break your Game?
Here’s my Experience.
At the start of the month, Unity announced that the latest version of their SDK would feature considerable upgrades to both the rendering engine and physics. The new version of Unity now features Nvidia’s PhysX 3.3 engine. The new engine is faster and more accurate than ever before. Check out this Gang Beasts video – the speed improvement is obvious.
Now I have to be honest, I did approach this update with some trepidation. The last Unity update made significant changes to the UI systems. While ultimately these were major improvements, it did render all my own UI code obsolete – and updating my existing UI to use the new Unity system took a lot longer than expected. I probably lost around a month due to the update.
I’m currently around 80% through developing a pool game engine (above) which, as you can imagine, is heavily dependent on the existing Unity PhysX system. Would the new physics engine break my game? Or would it improve it ten fold? I had no way of knowing. Ultimately, the lure of all those brand new features – particularly the Physical Based Lighting proved too hard to resist :). I went ahead with the update. And… well, I had a few problems. Actually, I had a lot of problems! So, I thought I’d do a quick post, just in case anyone else out there is experiencing similar physics upgrade problems in Unity 5.
The update itself was painless enough. I installed Unity 5 to a new directory so that I would be able to run the old Unity 4.6 if necessary. On loading my pool engine, Unity 5 performed an update on my code to make it compatible with Unity 5 language changes (which, as far as I can see, involved updating the code that applies Rigidbody forces to objects). So far so good.
On running the game, I came across an immediate problem. The rack of balls took on a life of their own – they immediately started moving away from each other! Very slowly – with no forces applied. When I attempted to strike the ball with the cue, the white ball barely made it to the triangle of balls. So it was kind of obvious that all my old physics set up – the forces applied, Physic Material Settings (Friction etc.), Rigidbody Settings (Angular Drag etc.) were now completely wrong and in need of updating.
I figured that with a bit of fine-tuning the game would be back up and running in no time. But I was wrong. I ran into a second problem. The balls themselves (with Sphere Colliders attached) just wouldn’t stop rolling. And no amount of changes to the physics settings seemed to help. I spent a great deal of time running tests, making updates – and getting nowhere.
Collider Mesh Problem
Eventually the penny dropped – it wasn’t actually the physics settings causing the problem. It was actually an anomaly with the collision detection.
Originally, the table collision was handled by a simple collision mesh. It wasn’t a convex mesh, it simply consisted of the table surface with the pockets built into it. This is the mesh I used:
One of the requirements of the new physics engine, is it requires Mesh Colliders to be convex. Being aware of this, I had already re-built all the table collision models so that each surface was convex – having volume – so should be okay. This is the updated collision:
But this didn’t fix the problem either. It was only after doing some independent tests with basic Sphere and Box Colliders that I realized that the collision mesh was the cause of the problem. Even when I replaced the table surface with a simple model of a 6-sided box, the balls continued to roll away by themselves – or failed to roll to a stop having been hit. The engine just didn’t seem to handle Mesh Colliders with the accuracy required for a game like this.
Final Solution – Use Compound Box Colliders Instead!
In the end, I found the only way to prevent the endless rolling / crawling was to do away with the Collider Meshes all together and create the collision using many basic Box Colliders – arranged in such a way as to mimic the original layout. There are 15 box colliders in total – here’s what they look like:
It turns out that adding multiple Box Colliders to an object is actually very straight forward. All you need to do, is create an Empty Game Object for each piece of box collision, then add the necessary Physics > Box Collider to each one. When you have all your Box Colliders positioned, make sure all the objects are children of the main object in the hierarchy, so it looks something like this:
Once I had done this, the physics engine started behaving properly. The balls no longer try to roll out of their racked position on start, and they roll to a halt as they should.
Despite making the above changes, my game physics still isn’t working as well as it was prior to the Unity 5 upgrade. I will put this down to the increased ‘realism’ of the new PhysX 3.3 engine. It’s going to be some time before it plays as well as it did before. It’s worth noting that I’ve also experienced problems with the following:
- High Velocity Balls Sticking to Cushions (intersecting collision?)
- Reduced shot accuracy when clipping balls (the AI is missing shots now where it hit them before)
- Occasionally balls don’t move when clipped at slow speeds
- Generally Erratic behavior that wasn’t present before (balls more prone to fly off the table!)
- Balls that bounce for no apparent reason (maybe when traversing between Box Colliders?)
I’m sure that all the above can be fixed, but it will take time. Thankfully all the Ray Casting functions appear to have held up (and there’s a LOT of ray casting going on in this engine!)
My advice to anyone contemplating the upgrade right now would be, that if your game is heavily reliant on physics (like mine) and you’re not far off completing it with Unity 4.6 – then think very carefully before you upgrade. Are the new features worth the time it will take to get your game fixed up again? Of course, you could just revert back to 4.6 after testing – and I did consider that. But that’s kind of an admission of defeat, isn’t it? 🙂 So be prepared to start again with those physics settings!