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

Parent - Child relationships with networked objects

Discussion in 'Multiplayer' started by brainwipe, Dec 30, 2020.

  1. brainwipe

    brainwipe

    Joined:
    Aug 21, 2017
    Posts:
    78
    Mirror (and MLAPI) both have a limitation that there can only be one Network identity (or Network object) in a hierarchy.

    This causes huge complexity when picking things up and dropping them.

    Example:

    A gun is a networked object, laying on the floor. It was Network spawned from a prefab with a integer member variable of 30 bullets. So far, so good.

    A player picks up the gun. The gun must become a child of the player camera. Mirror and MLAPI do not allow that.

    The solution is to have a separate "art only" prefab, which is instantiated on the player camera.

    However, in that process, the number of bullets in the gun is lost.

    This seems to be a common problem, perhaps a hangover from UNet. Will unity multiplayer 2021 efforts fix this?

    If not, I will need to restructure my game or abandon it.
     
    owlrazum likes this.
  2. owlrazum

    owlrazum

    Joined:
    Jun 20, 2020
    Posts:
    5
    I am also interested in this behaviour
     
  3. brainwipe

    brainwipe

    Joined:
    Aug 21, 2017
    Posts:
    78
    Since this post, I have done a fair amount of work toward this problem. I can't see any movement with Unity MLAPI to support this case. I've outlined the options in this video:

     
  4. PetRem_Labs

    PetRem_Labs

    Joined:
    Apr 21, 2020
    Posts:
    1
    Thanks for your workaround brainwipe.

    I have another solution using Netcode for Game Objects (Unity's new multiplayer system), for whoever reads that in the future:

    I have two players that are NetworkObjects, and a ball which is spawned by the server, which also is a NetworkObject, with a ClientNetworkTransform.
    Also, the ball has a script where we have a Transform to the hand holding it (null if nobody holds it).
    When one player takes the ball, I assign the Transform of its hand to the ball (either direct function or ServerRpc if client is taking it).
    Then in the ball script's Update function I simply set the position to the holdingHand position.

    That seems to work for me.
     
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi! As of NGO 1.0.0, you can parent NetworkObjects. Here's the documentation about it. Does this help?
     
  6. brainwipe

    brainwipe

    Joined:
    Aug 21, 2017
    Posts:
    78
    unity_KE7F8ZJogU7_Lw solution is exactly the same as I use in the video - you manually set the transform of the owned object.

    NGO 1.0.0, sadly, doesn't help.

    I've spent 5 months moving from Mirror to NGO and, sadly, NGO falls just short of what's needed for pick up and drop. The problem (as I've written elsewhere) is that you need the player's hand game object to be a network object so that the thing you're picking up can attach to it. BUT prefabs (which the player must be) cannot have nested game objects in them. So the hand can't be a nested game object. I know that the Boss Room does it using a constraint but the problem here is that you can't have Network Objects in hierarchies in prefabs.

    I explain it in more detail in this video (timestamp 4:51):



    Really looking forward to seeing a proper fix for this - or at least spreading this knowledge around the Unity support team so I don't have to explain it to Unity again.
     
    owlrazum likes this.
  7. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    its halfy done, its still a bug when a player wants to parent a network object under one of his tranform children that cant have a network object as it should be on the root of transform
     
  8. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Interesting...


    Do you mean something like a prefab hammer with its own network object cannot be made a child of the Pawn's hand bone because the Pawn has its network object at its root transform?
     
  9. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    not because the pawn has a networkobject, but because the hand doesnt and cannot have a network object i hope unity team will fix this
     
    brainwipe and MrBigly like this.
  10. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Are you able to disable the network object on the hammer whenever it is parented to the hand? Then it would be replicated by the hand's action, yes?
     
  11. liambilly

    liambilly

    Joined:
    May 13, 2022
    Posts:
    154
    i havent tried that, but i dont think it would work since without a network object its data wont be synched
     
  12. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    But isn't that the point?

    I would think that you can disable the hammer's network object while it is parented and enable it again when it goes orphaned.

    When you pick it up, it is indirectly synced by virtue of being in the hand of a sync'd pawn.

    When you drop it, the network object enables and it resync's on its own.

    But again, I may be wrong about all this. I am suggesting how it should work, but I have never tried working with the network objects myself.
     
  13. brainwipe

    brainwipe

    Joined:
    Aug 21, 2017
    Posts:
    78
    The problem is still that you need the hand to have an Network Object so that it can be a parent and you need the Player prefab top level to have a Network Object. You cannot have more than one Network Object in a prefab.

    A workaround is to spawn the hand onto the player after the player prefab has spawned. While that works, it makes a lot of the code complex and if the hand is a couple of objects down the hierarchy then each of the objects in the hierarchy need to be spawned in the same way because to be parented, you need to have a Network Object.

    It's got nothing to do with whether the hammer has a Network Object enabled or not.
     
    MrBigly likes this.
  14. MrBigly

    MrBigly

    Joined:
    Oct 30, 2017
    Posts:
    221
    Yep. I looked through the documentation provided above in this thread. It makes clear that what you described is not a bug, but how Unity intends for NGO to work with parenting scenarios. I apologize for not having looked sooner.
     
  15. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    98
    Just a heads up that I have been bashing my head against this problem and your threads on it came up, and I have a solution. The one from your video where you have a separate visual item for a held object and switch it on/off is appropriate for circumstances where you have generic items but I'm working on a game where you have to pick up and move objects that have ropes and physics objects attached, so it has to be the real actual in-world item that goes in the hand.

    I ran into all the same problems as you did, just posted the solution here: https://forum.unity.com/threads/player-hierarchical-networkobjects.1207012/ But the short version is:
    1. Picking up and dropping items is still handled by the server, and the item gets parented to the player's root (because as you discovered, that's really the only place it will actually let you put it).
    2. The items that can be picked up have a regular non-network monobehavior script on them. That script detects when the item has been parented to a player type object. It searches the player for the Hand transform, and each frame it moves the item to match the hand transform's position and rotation. We basically fake that it's in the hand without having to parent it to the hand.
    3. This is the important bit. In that same script on the item, we disable the item's NetworkTransform when it's picked up and re-enable it when the item is dropped. This stops the item from syncing while it's being held, so every observer sees the item exactly wherever it sees the hand bone is.
    Essentially, the visual placement of a held object can't be reliant on any network code, you only use the network for items that haven't been picked up and for the pick up and drop code. If you give anyone authority over the position the item is in, it'll only look right on their machine and everyone else will see it wrong. So instead the server just parents/unparents the item to the player roots and then any item that's being held autonomously "moves" itself to the correct place.
     
    MassimoFrancesco and brainwipe like this.