Search Unity

Picking Up / Changing Weapons Online

Discussion in 'Multiplayer' started by Obsolescence, Aug 1, 2017.

  1. Obsolescence

    Obsolescence

    Joined:
    Feb 19, 2017
    Posts:
    11
    I'm in a real pickle. I've spent all day trying to figure out how to:

    1) Spawn a prefab weapon item on the ground

    2) Alow any player to press a key to pick it up

    3) Instantiate the weapon prefab

    4) Parent the new prefab to the player and enable all players to see it (with rotation)

    I can make it all the way through #3 with no problem, and then I dumped 5+ hours into trying to get #4 to work with no luck. I've Googled and reread the networking docs. I've found almost-solutions and half-truths.

    How do online games with equippable weapons do it? Shooters, RPGs, anything... From what I can tell, the main problems are:

    1) You need a network ID to be able to spawn the weapon despite that child/parent objects aren't supposed to each have their own ID. So at some point I presume you must delete it, after Network Spawn?

    2) You can't use NetworkTransformChild without disabling/enabling the root object (in this case my player), because the awake function causes an error before a target can possibly be set.

    3) Even if you get all the way to having NetworkTransformChild hooked up, it doesn't seem as though there is any way to update the client's about the new object attached to the parent.

    If anyone has any references to guides on how to do this, or any code they could share, or any wisdom. This one really broke me today. It just doesn't seem like it should be this hard for something so many games do. Is there a way to immediately get the network ID of the object I just spawned (the weapon prefab) and then use that to parent the object on all clients?

    Is there a sad crying emoji to represent how lost I feel on this topic? If anyone thinks they might be able to help I can post my full code tomorrow, just leave a comment. Thanks for your time.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    One way to go around this is just to have already parented weapons attached to the player, and enabling them via server request (Cmd) and resending state via Rpc. (easiest way and bruteforce way)

    Other way is to ask a server via Cmd or Message to instantiate a weapon for a specific player, then - use Rpc or SendAll to notify other clients that client with id picked up such weapon. Sync the rotation and other stuff via SyncVars or Messages.

    You don't have to rely on NetworkTransform or NetworkTransformChild objects if you do the sync yourself. This applies to everything.

    Just pass the data you need, instantiate what you need and do what you need. Technicly speaking, I don't think that weapon should be spawned with NetId all around, since it's not how it should be implemented.
     
    TwoTen likes this.