#Struggling with Weapon system for multiple players

1 messages · Page 1 of 1 (latest)

heavy flint
#

https://paste.myst.rs/ussulmmg Hi im trying to make a simple multiplayer top down shooter. Im currently working on a combat component that works along side a gun controller which is just a gun. The guncontroller is a network behavior. My issue is on the player combat component im trying to create a function that adds these gun prefabs to the the player but im having real trouble with parenting and just general lack of understanding of the system. Any Help would be appreciated been working at this a couple days now. Ive Listed out the issues on line 154 of the combat controller

wind nexus
#

The gun prefabs will need to be network objects that are spawned by the server. There are a lot rules governing Network Object parenting

There is a new component called AttachableBehaviour that makes this quite a bit easier. But its brand new and there isn't much in the way of tutorials on it just yet

heavy flint
#

private void AddGunToInventory(GunController newGun, bool equip = true)
{
if (!IsServer)
return;

        int targetIndex = ownedGuns.Count < maxWeapons ? ownedGuns.Count : activeWeaponsSlot.Value;
        if (targetIndex < ownedGuns.Count && ownedGuns[targetIndex] != null)
        {
            ownedGuns[targetIndex].NetworkObject.Despawn();
            Destroy(ownedGuns[targetIndex].gameObject);
            ownedGuns[targetIndex] = null;
        }

        GunController gun = Instantiate(newGun, transform);
        NetworkObject networkObj = gun.GetComponent<NetworkObject>();


        networkObj.Spawn();
        
        if (targetIndex < ownedGuns.Count)
            ownedGuns[targetIndex] = gun;
        else
            ownedGuns.Add(gun);
        
        //newGunVisual.transform.localPosition = Vector3.zero;
        //newGunVisual.transform.localRotation = Quaternion.identity;
        
        gun.ShowVisuals(equip);
        
        if (equip)
            RequestActiveWeaponSlotChange(targetIndex);
    } is the prefab i showed above not a network object and this is my spawn code, the guns do get spawned in they just dont seem to be getting attatched?
#

or is there a better way that i should look into doing a weapon system?

wind nexus
#

After they are spawned, you have to call NetworkObject.TrySetParent() on the server and reset its local position.

The Attachable Behavior I linked above is a better way of doing this

heavy flint
#

ok ill look into that