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.

Question NetCode | Setting the parent of a NetworkObject to a deep-level nested child of the Player

Discussion in 'Netcode for GameObjects' started by o0_Chromium_0o, Nov 14, 2022.

  1. o0_Chromium_0o

    o0_Chromium_0o

    Joined:
    Sep 13, 2021
    Posts:
    5
    Hi.
    I'm using NetCode to create a multiplayer game. I want to grab a weapon in my game and set its parent to a child of my player (Gun holder). "Gun holder" itself is a child of Player children:

    GunAndGunHolder.jpg

    I tried many methods. And the only way I can think of is to spawn "GunHolderPivot" as a child of the player, And then spawn "GunHolder" as a child of the "GunHolderPivot". But this method is very time-consuming and restrictive. Especially if you have many nested children.

    * Note that I want the weapon to ease from its position to the "GunHolder" position and then set its parent. So I want to use the same GameObject as the weapon, and I don't want to instantiate a new weapon GameObject as the child of "GunHolder". *

    Any better solution?

    Thanks a lot.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    2,112
    Since NGO requires each parent to have a NetworkObject, it makes sense to keep the hierarchy as simple (flat) as possible.

    You could theoretically get rid of the GunHolderPivot if you provide a script that simply calculates the gun's attachment position, if necessary updating it every frame for example by getting the world position of a specific bone of the character's model and setting that to the position of the gun.

    As for animation, you'd simply set the gun's world position to where it was in the world when picked up, and then just lerp its local position to 0,0,0 if you use the pivot parent object. Without the pivot, you'd have to lerp towards whatever calculated position the gun should be at.

    If you don't need to synchronize gun position with other clients, for example it doesn't really matter to them where the gun is pointing at or it can simply be calculated by each client individually, you could make the pivot and gun non-networked, and only the gun on the ground is networked with the visibility state of the "gun item" synchronized to other clients. Whereas the picked up gun is a local instance of the gun that just follows along.
     
    o0_Chromium_0o likes this.
  3. o0_Chromium_0o

    o0_Chromium_0o

    Joined:
    Sep 13, 2021
    Posts:
    5
    Thank you for your reply!
    So. The point of keeping player and hierarchy simple and flat prevents many problems in the future of my project. Thanks

    I've already implemented the smooth movement of the weapon with a coroutine. And yes the animation and the position of the weapon are not important to sync when the weapon has been picked by the player.

    I'll try that local and networked gun prefabs method.

    I'm very grateful to you for all your help!