Hello Im making my first multiplayer game and so far i completed the basic fps tutorial for making it but now i added new bullet types and guns into the game but unfortunately they are only shown on the host’s game or so i think because he can shoot and his bullets can be seen but when other player’s shoot only ammo is wasted also guns do not appear when i switch them.I tried syncing most of the stuff between the host and the clients but im new to this stuff and if you could possibly explain i would be very happy.
This is my main code :
using UnityEngine;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class PlayerController : NetworkBehaviour
{
public Text Ammoleft;
[SyncVar]
public int Ak47Bullets = 30;
[SyncVar]
public int PipeShotgunBullets = 7;
[SyncVar]
public int currentWeapon;
public Transform[] weapons;
[SyncVar]
public string CurrentWeaponstr;
public GameObject ShottyBullet;
public GameObject Ak47Bullet;
public Transform bulletSpawn;
public float speed = 1; // speed in meters per second
void Update()
{
if (!isLocalPlayer)
{
return;
}
if(Input.GetKeyDown(KeyCode.Alpha1)) {
changeWeapon(1);
CurrentWeaponstr = "Ak47";
}
if(Input.GetKeyDown(KeyCode.Alpha2)) {
changeWeapon(2);
CurrentWeaponstr = "PipeShotgun";
}
float mouseInput = Input.GetAxis("Mouse X");
Vector3 lookhere = new Vector3(0,mouseInput,0);
transform.Rotate(lookhere);
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 3.0f;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Translate(x, 0, 0);
transform.Translate(0, 0, z);
Vector3 moveDir = Vector3.zero;
//moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
//moveDir.z = Input.GetAxis("Vertical"); // get result of WS keys in Z
// move this object at frame rate independent speed:
transform.position += moveDir * speed * Time.deltaTime;
if (Input.GetKeyDown (KeyCode.Mouse0) && CurrentWeaponstr == "PipeShotgun" && PipeShotgunBullets > 0)
{
CmdFireShotty();
PipeShotgunBullets -= 1;
Ammoleft.text = "Left: " + PipeShotgunBullets;
Debug.Log ("Shotpipe");
}
if (Input.GetKey (KeyCode.Mouse0) && CurrentWeaponstr == "Ak47" && Ak47Bullets > 0) {
CmdFireAk47();
Ak47Bullets -= 1;
Ammoleft.text = "Left: " + Ak47Bullets;
Debug.Log ("ShotAk");
}
}
// This [Command] code is called on the Client …
// … but it is run on the Server!
[Command]
void CmdFireAk47(){
if (CurrentWeaponstr == "Ak47") {
var Ak47bullet = (GameObject)Instantiate (
Ak47Bullet,
bulletSpawn.position,
bulletSpawn.rotation);
Ak47bullet.GetComponent<Rigidbody>().velocity = Ak47bullet.transform.forward * 6;
NetworkServer.Spawn(Ak47bullet);
}
}
[Command]
void CmdFireShotty()
{
// Create the Bullet from the Bullet Prefab
if (CurrentWeaponstr == "PipeShotgun") {
var Pipebullet = (GameObject)Instantiate (
ShottyBullet,
bulletSpawn.position,
bulletSpawn.rotation);
Pipebullet.GetComponent<Rigidbody>().velocity = Pipebullet.transform.forward * 6;
NetworkServer.Spawn(Pipebullet);
}
// Add velocity to the bullet
// Spawn the bullet on the Clients
}
public override void OnStartLocalPlayer ()
{
GetComponent<MeshRenderer>().material.color = Color.blue;
}
public void changeWeapon(int num) {
currentWeapon = num;
for(int i = 0; i < weapons.Length; i++) {
if (i == num) {
weapons *.gameObject.SetActive (true);*
-
}*
-
else*
_ weapons*.gameObject.SetActive(false); _
_ }_
_ }_
_}*_