Search Unity

Network Rigidbody Syncing

Discussion in 'Multiplayer' started by DChristy87, Oct 19, 2016.

  1. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    My players have grenade launchers so I'm instantiating and spawning an actual grenade prefab across to all players when a client shoots. Everything works okay for the most part. However, when a client is shooting and moving, relatively fast as they're in a car and not walking, then the location and rotation of the grenade will typically end up offset from the desired location, usually behind the player and the grenade ends up hitting the clients collider. The Server/Client will see the other players grenade instantiate and project normally but the grenade on the client who fires the shot will not behave properly.

    Any thoughts? If you need to see code, please let me know and I'll post what I have. Or if you just have a general idea why the client behaves this way, I'd appreciate some tips and feedback.
     
  2. Deleted User

    Deleted User

    Guest

    You can do it by follow, on prefab of your bullet Rigidbody isKinematic = true; When Server spawns you can uncheck isKinematic which is the best options to calculate physics on Server and just sync the transform of it. So add a component to your bullet NetworkTransform and switch mode to SyncTransform. Summary: we have server sided physics which is sync the correct transform for other clients.
     
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    This is like the tenth post dealing with networking projectiles. You don't need to Network.Spawn these. Instantiate them on each running instance of the game, and just have the server/host deal with collisions.
     
  4. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    Sorry, I had spent a considerable amount of time searching the webs for an answer but all the answers I found weren't consistent across. Can you elaborate a little on your answer with pseudo code?
     
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Well your implementation of it depends on how you're handling things. Animation Event. Input. Etc... Also if you're doing server authoritative or not... If it's input based and server authoritative...

    When the player presses a key send a Command to the server to fire the weapon.
    On the server first call an RPC that tells ALL clients to have that character fire the weapon.
    Fire the weapon on the server after the RPC is called.
    (( In both of these instances you should be creating the projectile locally. DON'T Instantiate it on the server and then NetworkServer.Spawn. You don't need to have a projectile synchronized (as long as you handle everything else right). ))
    In a script on the projectile have some sort of collision detection (OnCollisionEnter for example).
    On the server you handle damage inside the collision detection and destroy the projectile.
    On clients you only destroy the projectile in the collision detection.
     
  6. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    @DRRosen3 I appreciate the response and I think I'm pickin' up what you're layin' down. I happen to have it set up with Animation Event, but I do a check inside the function being called to check if the player is actually shooting or not because with the NetworkAnimation, all clients end up calling the function.
    So with my checks in place, (someone is pressing/holding the fire button, and the Animation Event called the fire function) Now that fire function will call a Command function. the Cmd function then immediately calls an RPC that has all clients instantiate a grenade with location, rotation and then sets it's velocity. Then right after the Rpc is called inside the Cmd, the Cmd will instantiate the grenade with all the same parameters? Then on the grenade, within the OnCollision I do a check like if(!isServer) {Destroy} else {give damage, blah, blah}

    If I'm having an explosion prefab be created from the location of where the grenade lands, should I instantiate that just the same as I did the projectile...with an Rpc? or would it be safe to call a NetworkServer.Spawn(explosion) for that?

    Edit: Actually thinking about it, shouldn't I just set it up to allow the Animator Event to just go ahead and call the function on all machines and that'll take care of the local instantiation of the projectile? and then set up the OnCollision to only apply damage if isServer?
     
    Last edited: Oct 19, 2016
  7. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    This is correct. If it's an Animation Event you just have the event cause the spawning of the projectile. Don't need any Commands or RPCs...UNLESS the server is authoritative. As far as the explosions go, inside the OnCollision just spawn the explosions locally. No need to network them.