How to properly synchronize wepaon switch between players
Options
I am trying to make players switch weapons they have (And show it to other players). I am doing it through activating/deactivating player's children in respective slots of weapon models. But no matter what i do, weapon models don't appear and don't change
Code fore weapon switch:
Code fore weapon switch:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using Photon.Realtime;
public class player_weaponequip : MonoBehaviourPunCallbacks
{
[SerializeField] item_mainItem[] items;
public GameObject player;
PhotonView view;
int itemIndex;
int previousitemIndex = -1;
void Awake()
{
view = player.GetComponent<PhotonView>();
}
void Start()
{
if (view.IsMine)
{
equipItem(0);
}
}
void equipItem(int _index)
{
if (view.IsMine)
{
if (_index == previousitemIndex)
return;
itemIndex = _index;
items[itemIndex].itemGameObject.SetActive(true);
if (previousitemIndex != -1)
{
items[previousitemIndex].itemGameObject.SetActive(false);
}
previousitemIndex = itemIndex;
}
}
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
if (view.IsMine)
{
weaponSwitch();
}
}
public void weaponSwitch()
{
if (view.IsMine)
{
for (int i = 0; i < items.Length; i++)
{
if (Input.GetKeyDown((i + 1).ToString()))
{
equipItem(i);
break;
}
// Debug.Log(i);
}
if (Input.GetAxisRaw("Mouse ScrollWheel") < 0f)
{
if (itemIndex >= items.Length - 1)
{
equipItem(0);
}
else
{
equipItem(itemIndex + 1);
}
}
if (Input.GetAxisRaw("Mouse ScrollWheel") > 0f)
{
if (itemIndex <= 0)
{
equipItem(items.Length - 1);
}
else
{
equipItem(itemIndex - 1);
}
}
}
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
{
if (!view.IsMine && targetPlayer == view.Owner) //Подтверждает, что мы синхронизировали оружия
{
equipItem((int)changedProps["itemIndex"]); //Синхронизирует оружия между всеми игроками
}
Debug.Log("Recieved item");
}
}
0