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

Instantiate Prefab immediately on Client, then spawn on server?

Discussion in 'Multiplayer' started by dgoyette, Sep 28, 2017.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    I'm trying to understand whether there's a way to deal with fast-moving projectiles fired by the player that don't feel sluggish and laggy.

    I'm creating my projectiles in a very standard way. When my player shoots, it fires a fast-moving projectile forward. I'm current executing a server [Command] method that instantiates a prefab, and then calls
    Code (CSharp):
    1. NetworkServer.Spawn()
    to spawn the projectile on all clients. When I'm playing as the Host client, the projectile spawns instantly, and there is no lag as it flies away from me. But when I'm playing as a Remote client, there is a noticeable delay before the projectile appears. This makes sense, given that the request has to go to the server, and then back to the client, before I see the projectile, but it feels awful as a player.

    Despite the following being a potential cheating exploit, here's what I was hoping to be able to do: Upon firing a projectile, instantiate it on the firing player's client immediately, and give client authority to the projectile, so that it appears instantly in front of the player and moves smoothly away. But after instantiating it locally, it should be sent to the server and spawned so the other players see the projectile.

    I tried playing around with this, instantiating my prefab on the remote client, then passing the GameObject to a server [Command] to spawn it, but the game object always ends up being null when the [Command] is executed on the server. This is despite my prefab having a NetworkIdentity. (My understanding is that you can pass a GameObject to a [Command] as long as it has a NetworkIdentity.) But perhaps only the server properly creates NetworkIdentities?

    Anyway, does anyone have any ideas on this? Again, the main priority is for the player firing the projectile to have that experience be clean and smooth regardless of whether they the host or remote.
     
  2. Ktulian

    Ktulian

    Joined:
    Apr 6, 2014
    Posts:
    8
    I had the same issue. One way to get around this is to spawn (with authority) the weapon and or projectiles when that item is equipped but spawn the projectiles disabled in some form and pool them. This way equipping the weapon may have a slight delay but firing projectiles won't.
     
  3. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You could also just spawn it on the client and make it move into the shooting direction, and call a Cmd to tell the server to do the same. In other words: simulate it on the server AND on the client, while assuming that the results are probably the same (and if not, the server can still correct the client's result).