Hi all
im making a small fps multiplayer using the unity multiplayer stuff. i have made it so you can switch weapons from the choice of two. i have two game objects one is a assault rifle and one is a sniper. i have attached a gun script to both that has fire rate and range. this works all fine. To change gun i simply disabled the gun script for that certain gun and also disabled the mesh render for that gun. I was having the problem where
if you switch weapon would switch all the clients weapons visually. fixed this now tho but now im having the problem of syncing what weapon is currently out. Im not sure how to go ahead and do it as i know i need to use [synvar] and [clientrpc] i think but i dont know how to go around it. thanks for your help
i have included the script i made to change weapons. i know its probably quite basic and i have tried to do some research on it but im really not too sure about this one thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SwitchGuns : NetworkBehaviour {
NetworkIdentity NetworkIdentity;
GunScript AkScript;
GunScript SniperScript;
MeshRenderer AKRender;
MeshRenderer SniperRender;
// Use this for initialization
void Start()
{
//find components
NetworkIdentity = GetComponentInParent<NetworkIdentity>();
if (NetworkIdentity.isLocalPlayer == true)
{
AkScript = transform.Find("AK-47").GetComponent<GunScript>();
SniperScript = transform.Find("L96_Sniper_Rifle").GetComponent<GunScript>();
AKRender = transform.Find("AK-47").GetComponent<MeshRenderer>();
SniperRender = transform.Find("L96_Sniper_Rifle").GetComponent<MeshRenderer>();
//initilise varables on spawn
AkScript.enabled = true;
SniperScript.enabled = false;
AKRender.enabled = true;
SniperRender.enabled = false;
}
}
// Update is called once per frame
void Update()
{
if (NetworkIdentity.isLocalPlayer == true)
{
//ak enabled
if (Input.GetButtonDown("PrimaryGun"))
{
SniperScript.enabled = false;
AkScript.enabled = true;
AKRender.enabled = true;
SniperRender.enabled = false;
}
//sniper enabled
if (Input.GetButtonDown("SecondaryGun"))
{
AkScript.enabled = false;
SniperScript.enabled = true;
SniperRender.enabled = true;
AKRender.enabled = false;
}
}
}
}