Sync weapon switching uNet

Updated on May 27, 2017 in [A] Multiplayer
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on May 27, 2017

Hello guys, I’m trying to sync the weapon switching in my FPS (i’ve kinda followed the Brackeys tutorials).
Here is the way I am handling the weapon switching:


[SerializeField]

private Camera weaponCamera;

[SerializeField]

private GameObject weaponHolder;
private const string LOCAL_WEAPON_LAYER = "Weapon";
private int defaultWeapon = 0;

private WeaponClass currentWeapon;
private void Start()

{

EquipWeapon(defaultWeapon);

}
private void Update()

{

if (Input.GetKeyDown(KeyCode.Alpha1))

{

EquipWeapon(0);

}

if (Input.GetKeyDown(KeyCode.Alpha2))

{

EquipWeapon(1);

}

//if (Input.GetKeyDown(KeyCode.Alpha3))

//{

// EquipWeapon(2);

//}

//if (Input.GetKeyDown(KeyCode.Alpha4))

//{

// EquipWeapon(3);

//}

}
private void EquipWeapon(int _weaponIndex)

{

int i = 0;
foreach(Transform _weapon in weaponHolder.transform)

{

if(_weaponIndex == i)

{

_weapon.gameObject.SetActive(true);
if (isLocalPlayer)

{

foreach (Transform _part in _weapon)

{

_part.gameObject.layer = LayerMask.NameToLayer(LOCAL_WEAPON_LAYER);

if(_part.childCount != 0)

{

foreach(Transform _subpart in _part.transform)

{

_subpart.gameObject.layer = LayerMask.NameToLayer(LOCAL_WEAPON_LAYER);

}

}

}

}

currentWeapon = _weapon.gameObject.GetComponent<WeaponClass>();
weaponCamera.fieldOfView = currentWeapon.GetFOV();
//Debug.Log(currentWeapon.gameObject.GetComponent<WeaponClass>().GetName() + " activated.\nDamage : " + currentWeapon.gameObject.GetComponent<WeaponClass>().GetDamage() + " / FireRate : " + currentWeapon.gameObject.GetComponent<WeaponClass>().GetFireRate());

}

else

{

_weapon.gameObject.SetActive(false);

}
i++;

}

}

Any suggestions on how to do it ?
Thanks for stopping by !

  • Liked by
Reply