- Home /
Switching weapons with PUN 2
I can't seem to get this to work. The player presses 1 or 2 or 3 etc depending where the weapon is on the hotbar and it will show the weapon by setting it active in the hierarchy. everything works fine client side obviously but I cannot get it to show from the other players perspective. Not sure where to put the photon view or where to put the code exactly but this is my code that is currently on the hotbar slot.
Also worth mentioning that the item is selected from an actual inventory and not just a simple switch so it all corresponds to another bit of code that is the item script attached to the pickupable item.
Thanks if advanced if anyone can point me in the right direction.
using System.Collections; using System.Collections.Generic; using UnityEngine; using Photon.Pun; using Hashtable = ExitGames.Client.Photon.Hashtable; using Photon.Realtime;
public class HotbarSlot : MonoBehaviourPunCallbacks { public GameObject[] unstaticItems;
public static GameObject[] items;
public Inventory inv;
public NewPlayerHealth updateVitals;
public AudioSource audioSource;
public AudioClip Eating;
public KeyCode key;
public PhotonView PV;
[SerializeField]
private GameObject placeableObjectPrefab;
[SerializeField]
private GameObject currentPlaceableObject;
private float mouseWheelRotation;
void Awake()
{
PV = GetComponent<PhotonView>();
}
void Start()
{
if (unstaticItems.Length > 0)
items = unstaticItems;
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (!PV.IsMine) return;
if (Input.GetKeyDown(key))
Equip();
if (Input.GetKeyDown(key))
{
EatFood();
}
PlaceFurnace();
RotateFromMouseWheel();
}
void Equip()
{
{
photonView.RPC("EquipRPC", RpcTarget.AllBuffered);
}
}
[PunRPC]
void EquipRPC()
{
{
if (transform.childCount > 1)
{
Item item = transform.GetChild(1).GetComponent<Item>();
if (item.equipmentType == "Hand")
{
for (int i = 0; i < items.Length; i++)
{
if (i == item.equipmentIndex)
{
items[i].SetActive(!items[i].activeInHierarchy);
}
else
{
items[i].SetActive(false);
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Weapon Switching 2 Answers
Making weapons animations for first person shooter 2 Answers
how to i play another animation when the first one is finished 1 Answer
Weapon switching using array 1 Answer
Weapon Switching Help 1 Answer