Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Funky Multiplayer Physics Collisions

Discussion in 'Multiplayer' started by SHiLLySiT, Nov 5, 2014.

  1. SHiLLySiT

    SHiLLySiT

    Joined:
    Feb 26, 2013
    Posts:
    12
    We initially setup a vehicle with a single box collider and four wheel colliders, then followed this tutorial to get multiplayer going. It worked great except for when two cars (belonging to different clients) would collide - they'd bounce into the air or get sent off in another unrealistic direction. Often it gives the effect that cars are using each other as ramps or trampolines. Collisions with anything else that wasn't controlled by a player reacted fine. We've now switched to Randomation Vehicle Physics and Photon Unity Networking (PUN), but the collision problem still persists.

    We're planning on eventually setting up a server that simulates physics and then updates any clients that are out of sync (while also keeping local simulations for predication). Would this solve our problem or is there something else wrong with how we have the car physics or multiplayer setup?

    I've uploaded a build of our game here: http://beta.bulletproofarcade.com/temp/Build.html

    Instructions:
    1. After the game loads, if a "Join TestRoom" button appears, click that. Otherwise click the "Start Server" button
    2. Load the game in another tab and follow step one again
    3. Try crashing into one of your cars
    Controls:
    • Up: Accelerate
    • Down: Decelerate
    • Left/Right: Steer
    • Space: Handbrake
    • R: Reset car
    • Y: Respawn car
    If need to see any particular scripts or project settings let me know.
     
  2. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    Authoritative server is the way to go, with all physics calculated on the server, and the resulting transforms sent to the clients. This is how I got my game running well, although this was with uLink.
     
  3. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @Meltdown: Did you configure all Rigidbodies on the client as kinematic in that case?
    Afaik, the problem happens when an incoming update sets the position for some remote-controlled Rigidbody. It gets set to an absolute position which might be overlapping with another object (because the position is a bit outdated and because the local simulation has slightly different results). As physical objects can't intersect, the engines assumes they had quite some impact instead and gives them forces which shoots the objects around.
    I'm not sure if you can solve these issues for all cases. An authoritative server with physics gives the best results but also requires more work on your servers.
     
  4. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    @tobiass Funny enough, I never needed to, uLink had a smoothrigidbody script I used on the clients and it handled this all automatically.

    I got 200 race sessions running concurrently on an entry level server, and this was with an unoptimised server/physics scene running 16 wheel colliders and loads of mesh colliders, but yes.

    It definately requires more CPU overhead, but the fact it all just works and works smoothly is a big win. With Unity 5 Physx server performance will be even more impressive.
     
  5. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    PhysX not being deterministic is another problem of running any physics client side for the players not controlling a Unit.

    We are using PUN for our project, as our voxel world would be to expensive to use server side for an indie game like ours.
    We let the client control his own player using PhysX (and a lot of raycasting) and then update the other clients with position/rotation/other stuff. All other players than your own will be kinematic. Works well even for a fast-paced game like ours.

    But we can't have more than 4 players because we need to update each player often enough to make sure it fits our game (20 msgs per second), and there is a 500 msg limit.
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @Meltdown: I guess you could optimize with a simplified version of the levels and everything.
    @BFGames: Good to read you could make it work well for your game. The msg/sec limit in a room is also a technical barrier in most cases. Did you experiment with less updates/sec and more interpolation?
     
  7. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    We did, but our game is really fast paced and the velocity (HIGH velocity) and directions changes so rapidly that you quickly get out of sync on few lost messages. And yes we played A LOT around with inter/extrapolation. But 4 players is okay for our game, and levels are designed around it now. Could be fun to add a few more, but its fine.
     
  8. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    @BFGames: Thanks for the info. We will keep looking for ways to optimize messages but I can't promise anything yet. If we can make it, maybe it's time for a part 2 of your game :)
     
  9. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Hehe, next step would be a fully authoritative setup without a voxel world! But we don't have money for that yet.
     
  10. jachitla

    jachitla

    Joined:
    Jul 13, 2015
    Posts:
    12
    @Meltdown Hey, I am using uLink now for my game which uses authoritative server to handle all physics. I dont know why but when my car crashes into another car, it will either push it a little or not at all. Is there specific settings for the rigidbody I need to set.

    FYI: I have the three prefabs- CarCreator (for server), CarOwner (for local), and CarProxy (for other players) all with network views and smoothrigidbody improved scripts on them. They also all have rigidbodies but only the car creator has the collider on it. Then of course the owner has the input sender script and the creator has the receiver script and handles the inputs.

    Thanks,
    Jason
     
  11. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    There is no perfect art form to implementing it. You'll need to play around and see what works. I found I had the best results with colliders on every prefab (if it's an authoritative server). I also found running the vehicle physics package on the client too also improved things (which is contrary to what most people recommend against). Just experiment, and think outside the box until you find something you're happy with.

    Also play with the bounciness of the colliders, and the mass of the vehicles.

    Also remember to use Network Emulation, and make sure the game plays well at 300ms ping too.
     
  12. jachitla

    jachitla

    Joined:
    Jul 13, 2015
    Posts:
    12
    Okay, I'll experiment around a bit more...

    Also, the uLink physics tutorial says not to implement client side prediction as the delay may seem kind of natural to players but did you implement client side prediction for your game? Im assuming you probably did as you said you have colliders and physics package running on each prefab and not just the server owned one.
     
  13. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,816
    I didn't implement client-side prediction, but I did find having physics running on the client too in my case seemed better.
     
  14. jachitla

    jachitla

    Joined:
    Jul 13, 2015
    Posts:
    12
    How can you have physics running on client, if authoritative server is just going to tell client where to be each frame?