Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating a bullet and linking it to a client bullet

Discussion in 'Multiplayer' started by Zephilinox, Nov 30, 2016.

  1. Zephilinox

    Zephilinox

    Joined:
    Sep 15, 2014
    Posts:
    14
    Hello,

    I'm trying to spawn a bullet when the client fires. I'd like to do some client-side prediction on this bullet until I hear back from the server. I use a Command call to tell the server to spawn the bullet.

    I'm not sure how I would link the two together though. When I instantiate the local non-networked version of the bullet and begin doing prediction on it and then hear back from the server how can I grab the bullet it has spawned and use that information to replace the bullet I have already instantiated?

    Sorry, I hope this question makes sense.

    Alternatively if there's any shooter tutorials that don't use the network transform component that would be helpful as well.
     
  2. Jos-Yule

    Jos-Yule

    Joined:
    Sep 17, 2012
    Posts:
    292
    Yeah, you can't really do "client side spawning" using the UNET high-level api. If the bullet's path is reasonably deterministic, you can just do a CMD to tell the server to shoot the same way you did on the client, and then have the server tell (via RPC) all the other clients to do the same. All the bullets would be local. Depending on, well, lots of things, your simulation will be more-or-less the same on all the computers. Only 1 should be responsible for dealing with hit detection and damage however, usually the Host/Server. But then you have to manually deal with clean up of that bullet, rather then using the spawned item's NetworkDestroy(). You can also send updates from the "owner" of the bullet to the server to the other clients, and those updates are position updates from where the bullet is on the owners computer - so it becomes the authority for that bullet. You'd have a bunch of messages to send - FireBullet(), BulletPositionUpdate() and KillBullet(). So so so many options on how to do it. If you don't want to wait for the Host to spawn the bullet before you see it on your client, you are going to have to do some kind of scheme like this.
     
  3. Zephilinox

    Zephilinox

    Joined:
    Sep 15, 2014
    Posts:
    14
    Ah! So I shouldn't be using the network spawn functionality. Instead I create a local bullet and tell the server to do the same, and then the server tells all the clients to do the same.

    If the delay between all clients is 100ms then the bullet that client A creates will be 100 ms ahead of the server and 200 ms ahead of client B. I suppose the best thing to do is let client A handle what this bullet hits as I'm not worried about cheating and then inform the server so it can propagate the relevant information to all the other bullets.

    Thank you, I hadn't thought of creating my own spawning system to handle this. It's a little awkward but I'm sure I'll figure it out.