Hello, I am quite new to unity multiplayer, I have been working on this project for about a week now, and I have encountered a devastating problem that prevents me from moving forward. I will post my script down below.
btw host is working fine only the remote clients arent working (for everything)
When the host spawns bullets everything works perfectly, but when the clients spawn it, this line doesn’t effect it
bullet.GetComponent<BulletScript>().damage = 10;
and I also have a switching weapons part at the top and that switches the shooting stats of the script ( holds which bullet to spawn too so i can have different models for the bullets and so the weapons can be parented to the player)
everything in that part works except it doesnt change the bulletPrefab, and the bulletparticles prefab.
gs is gun script which the script that holds the stats of shooting, and is attached to the gun
this part doesnt work either, again only for clients
if (isLocalPlayer)
{
bullet.GetComponent<MeshRenderer>().enabled = false;
bulletParticles.GetComponent<MeshRenderer>().enabled = false;
}
Here is the full script, I know its kind of messy.
C# only plz
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Shoot : NetworkBehaviour
{
public GameObject gun;
public GameObject gunHolder;
public GameObject visor;
public bool reloading;
public GunScript gs;
public GameObject[] weapons;
public int currentWeapon;
public GameObject equippedWeapon;
public string shootAnimation;
public string reloadAnimation;
// Use this for initialization
void Start()
{
equippedWeapon = weapons[0];
gs = equippedWeapon.GetComponent<GunScript>();
if (isLocalPlayer)
{
shootAnimation = gs.shootAnimation;
visor.gameObject.GetComponent<MeshRenderer>().enabled = false;
gameObject.GetComponent<MeshRenderer>().enabled = false;
}
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (currentWeapon > weapons.Length - 1)
{
currentWeapon = 0;
}
if (Input.GetKeyDown(KeyCode.E))
{
currentWeapon += 1;
}
gs = equippedWeapon.GetComponent<GunScript>();
equippedWeapon = weapons[currentWeapon];
gs.reloadTimer -= Time.deltaTime;
if (!isLocalPlayer)
{
return;
}
if (gs.ammo < gs.magazineSize)
{
if (Input.GetKeyDown(KeyCode.R))
{
gs.reloadTimer = gs.reloadTimerSet;
reloading = true;
gs.gunHolder.GetComponent<Animation>().Play(gs.reloadAnimation);
}
}
if (reloading == true)
{
if (gs.reloadTimer < 0)
{
gs.ammo = gs.magazineSize;
reloading = false;
}
}
gs.fireRate = gs.fireRate - Time.deltaTime;
if (gs.ammo > 0)
{
if (reloading == false)
{
if (gs.semiAutomatic)
{
if (Input.GetButtonDown("Fire1"))
{
if (gs.fireRate < 0)
{
gs.ammo = gs.ammo - 1;
CmdFire();
Fire();
gs.fireRate = gs.fireRateSet;
}
}
}
if (!gs.semiAutomatic)
{
if (Input.GetButton("Fire1"))
{
if (gs.fireRate < 0)
{
gs.ammo = gs.ammo - 1;
CmdFire();
Fire();
gs.fireRate = gs.fireRateSet;
}
}
}
}
}
}
[Command]
void CmdFire()
{
var bullet = (GameObject)Instantiate(gs.bulletPrefab, gs.bulletSpawn.position, gs.bulletSpawn.rotation);
var bulletParticles = (GameObject)Instantiate(gs.bulletParticlesPrefab, gs.bulletParticleSpawn.position,gs.bulletParticleSpawn.rotation);
if (isLocalPlayer)
{
bullet.GetComponent<BulletScript>().damage = 10;
bullet.GetComponent<MeshRenderer>().enabled = false;
bulletParticles.GetComponent<MeshRenderer>().enabled = false;
}
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * gs.shootSpeed;
bulletParticles.GetComponent<Rigidbody>().velocity = -bulletParticles.transform.right * 1;
bullet.transform.eulerAngles = new Vector3(bullet.transform.eulerAngles.x + Random.Range(-gs.accurracy, gs.accurracy), bullet.transform.eulerAngles.y + Random.Range(-gs.accurracy, gs.accurracy), bullet.transform.eulerAngles.z + Random.Range(-gs.accurracy, gs.accurracy));
NetworkServer.Spawn(bullet);
NetworkServer.Spawn(bulletParticles);
Destroy(bullet, gs.range);
Destroy(bulletParticles, gs.range);
}
void Fire()
{
gs.gunHolder.GetComponent<Animation>().Play(gs.shootAnimation);
var localBullet = (GameObject)Instantiate(gs.bulletPrefab, gs.bulletSpawn.position, gs.bulletSpawn.rotation);
var localBulletParticles = (GameObject)Instantiate(gs.bulletParticlesPrefab, gs.bulletParticleSpawn.position, gs.bulletParticleSpawn.rotation);
localBullet.transform.eulerAngles = new Vector3(localBullet.transform.eulerAngles.x + Random.Range(-gs.accurracy, gs.accurracy), localBullet.transform.eulerAngles.y + Random.Range(-gs.accurracy, gs.accurracy), localBullet.transform.eulerAngles.z + Random.Range(-gs.accurracy, gs.accurracy));
localBullet.GetComponent<Rigidbody>().velocity = localBullet.transform.forward * gs.shootSpeed;
localBullet.tag = "LocalBullet";
localBulletParticles.GetComponent<Rigidbody>().velocity = -localBulletParticles.transform.right * 1;
Destroy(localBullet, gs.range);
Destroy(localBulletParticles, 2.5f);
}
void OnGUI()
{
if (!isLocalPlayer)
{
return;
}
GUI.color = Color.black;
GUI.Label(new Rect(Screen.width * 0.8f, Screen.height * 0.85f, 100, 20), "Ammo =" + gs.ammo);
GUI.color = Color.yellow;
GUI.Label(new Rect(Screen.width * 0.77f, Screen.height * 0.815f, 100, 20), "Weapon: " + gs.gunName);
}
}