Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unity Multiplayer spawning grenades with local player authority

Discussion in 'Multiplayer' started by robochase, Dec 11, 2016.

  1. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    228
    i want clients to be able to throw grenades, and have it look super smooth for them. the pattern i use for bullets is kinda like this -

    1. client presses fire
    2. client sends command to server CmdFire()
    3. server spawns bullet.
    4. client(s) eventually get notice of the spawned bullet, and the bullet appears.
    5. the bullet travels along at a constant velocity, so its traversal kind of just works and stays in sync kinda for server & client.

    My current problem: because there's a round trip to the server before you are allowed to see the spawned bullet, this can feel pretty bad for the player who shot the bullet (not a deal breaker so far - i can mask the delay a bit with muzzle flare vfx).

    in my game, with grenades, this technique feels especially terrible, though. i'd like the client to have authority over the rigidbody for the grenade the instant he throws it. so the instant he presses throw, he sees the grenade leave his hand and act like a rigidbody being thrown into the environment. without hiccups or weird moments where the grenade hasn't spawned on the server yet.

    is there an easy way to handle this scenario with unet? i tried checking 'Local Player Authority' on the grenade's network identity, but that didn't automagically fix it. i'm guessing i'd need to check local player authority AND change the client code somehow.

    one painful road i could go down -
    1. client presses fire.
    2. client spawns a projectile and starts simulating rigidbody stuff. he also gives this projectile a temporary localProjectileId
    3. client sends command to server CmdFire(int localProjectileId)
    4. server spawns projectile. it will eventually appear for other clients.
    5. server grants the client authority of the projectile via NetworkIdentity.AssignClientAuthority
    6. server sends message to the client who fired: ThisIsYourBullet(uint networkId, int localProjectileId), passing back the localProjectileId that was sent with the initial CmdFire call, as well as the networkId for the projectile that was just spawned
    7. client gets the ThisIsYourBullet message. he uses the networkId to find the Network'd projectile, and he uses the localProjectileId to find the projectile he created in step 2.
    8. client moves the networked projectile to where his local projectile currently is. he manually sets the rigidbody's velocity of the network projectile to match the local projectile.
    9. client destroys his local projectile since it's no longer needed.
    i think it works on paper, but it feels kind of convoluted. and i kind of wonder if it would end up looking ok.

    any other ideas? this feels like it would be a pretty common problem for multiplayer games where clients are meant to spawn stuff, but it gets delayed by a round trip to the server.
     
  2. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    86
    Are you sure you actually want client authority on the granade?

    This means you would allow the clients to move them anywhere, instantly.

    I think you might be overcomplicating it.
     
  3. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    228
    this is for a VR game, so keeping things looking very fluid to the person performing the actions is my #1 goal.

    i've played plenty of peer to peer multiplayer where the client is in control of their projectiles. this is the gameplay feel i want.

    if cheating becomes an issue, there's ways i can have the server approximate the validity of client commands. i.e. the server can simulate its own version of the grenade locally, and tolerate a certain amount of time&distance between when their sim and where/when the client said the explosion should take place. or it could more broadly check if the grenade traveled too far outside of a radius from where it was fired, since even a perfectly placed shot will only ever travel so far.
     
    gamedev42 likes this.
  4. gamedev42

    gamedev42

    Joined:
    Jan 17, 2017
    Posts:
    3
    I think there is no other option for you than to go down the road you described. You have to spawn the grenade locally (not a networked object), send command to the server, telling it to create the networked grenade with initial position and direction. Then, your local client will perceive the visuals of your local grenade, while the rest of the clients will (inevitably) see your grenade spawn a bit off of your hand, because it is physically impossible for them to receive your intent sooner than 2 pings have passed. This is easier, when you have already existing object and must interpolate its position, for example. But in the case of spwaning/deleting the objects I think this is how you do it basically.
     
  5. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    86
    Actually, this can be avoided, in case there is a throw animation.
    As soon as the animation starts, the server can be informed of the client's intention of throwing a granade, so they can somehow coordinate.
    While this is true for non-vr games, I believe this could be applied at VR too, if you try hard enough.
     
  6. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    228
    hey, this is still being talked about!

    I ended up doing exactly what i described. it works!
     
  7. ristophonics

    ristophonics

    Joined:
    May 23, 2014
    Posts:
    31
    Last edited: Jul 26, 2018
  8. MildGravy011

    MildGravy011

    Joined:
    Oct 23, 2013
    Posts:
    1
    Do you have any sample of this script? It would be greate if you could share it.
    I just can't understand how it should works
     
unityunity