Question by
Gugoasa · May 26, 2017 at 06:31 PM ·
transformnetworksynchronization
Sync weapon switching uNET.
Hello ! I'm trying to sync the weapon switching on the network, so every one can see what weapon is equiped on the other players. Right now the code is this :
[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++;
}
}
I've tryied to put the "isLocalPlayer" in the Update function but it doesnt work.
Hierarchy situation :
Help ? Thanks guys !
hierarchy.png
(71.0 kB)
Comment
Your answer
Follow this Question
Related Questions
Unet rigidbody.AddForce not working for clients 1 Answer
Question to RPC and Command (Unet) 0 Answers
Network Transform synchronizes only rotation but not movement 1 Answer
Network Animations 0 Answers