- Home /
Clients can't spawn bullets, but the server does
Hi all,
I'm trying to develop a multiplayer project and I have a problem with spawning GameObjects on client. Here is the code I use: using System; using System.Collections; using UnityEngine; using UnityEngine.Networking;
public class WeaponController : NetworkBehaviour
{
[SerializeField] private GameObject firePosition;
[SerializeField] private ShotEffectsManager[] laserGuns;
[SerializeField] private ShotEffectsManager rocketLauncher;
float nextFire;
float nextEffectFire;
float nextRocketFire;
private bool canShoot;
bool laserShot = false;
bool rocketLaunched = false;
void Start()
{
for(int i =0; i < laserGuns.Length; i++)
{
laserGuns[i].Initialize();
}
rocketLauncher.Initialize();
if (isLocalPlayer)
canShoot = true;
}
void Update()
{
if (!canShoot)
return;
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + laserGuns[0].fireRate;
laserShot = true;
rocketLaunched = false;
CmdProcessShotEffects();
}
if (Input.GetButton("Jump"))
{
rocketLaunched = true;
laserShot = false;
CmdProcessShotEffects();
}
}
[Command]
void CmdProcessShotEffects()
{
if (!NetworkServer.active)
{
Debug.Log("SERVER IS NOT ACTIVE!!");
return;
}
else
{
Debug.Log("Server Active.");
}
if (laserShot)
{
if (Time.time > nextEffectFire)
{
nextEffectFire = Time.time + laserGuns[0].fireRate;
for (int i = 0; i < laserGuns.Length; i++)
{
Debug.Log("Before instantiating");
laserGuns[i].SetShotEffects();
GameObject go = Instantiate(laserGuns[i].ammoPrefab, laserGuns[i].transform.position, laserGuns[i].transform.rotation) as GameObject;
Debug.Log("Laser: " + go);
NetworkServer.Spawn(go);
Debug.Log("Networked Spawned");
}
}
}
if (rocketLaunched)
{
rocketLauncher.SetShotEffects();
if (Time.time > nextRocketFire)
{
nextRocketFire = Time.time + rocketLauncher.fireRate;
Debug.Log("Before instantiating Rocket");
GameObject instance = Instantiate(rocketLauncher.ammoPrefab, rocketLauncher.transform.position, rocketLauncher.transform.rotation) as GameObject;
Debug.Log("Rocket: " + instance);
NetworkServer.Spawn(instance);
Debug.Log("Networked Spawned Rocket");
}
}
}
}
I've watched tutorials and added a lot of Debug.Logs in order to see what's happening there and it seems that CmdProcessShotEffects() is never called on clients I think. Everything is done right: both laser and rocket prefabs are spawnable prefabs in the NetworkManager and both have NetworkIdentity and NetworkTransform components.
I also have an unusual warning that sais:
" Did not find target for sync message for 14 //This number is different every time I shoot UnityEngine.Networking.NetworkIdentity: UNetStaticUpdate() "
And I don't know what it means.
Can anyone help me understand this issue, please?
Answer by Slastraf · May 29, 2018 at 10:48 AM
have you found a clue ? I have the same problem...
Hi,
Well.. it's been a long time. I have changed some things, but I don't say it's the best way to fix the problem.
void Update()
{
if (!canShoot)
return;
if (Input.GetButton("Fire1") && Time.time > nextFire && ammo$$anonymous$$anager.laserInUse.GetQuantity() > 0)
{
nextFire = Time.time + laserGuns[0].fireRate;
CmdProcessShotEffects();
PlayerCanvas.canvas.UpdateLaserAmuont(ammo$$anonymous$$anager.laserInUse.GetQuantity(), 5000);
}
if (Input.GetButton("Jump") && ammo$$anonymous$$anager.rocketInUse.GetQuantity() > 0)
{
CmdProcessRocketEffect();
PlayerCanvas.canvas.UpdateRocketAmuont(ammo$$anonymous$$anager.rocketInUse.GetQuantity(), 100);
}
}
[Command]
void CmdProcessShotEffects()
{
RpcProcessShotEffects();
}
[ClientRpc]
void RpcProcessShotEffects()
{
Debug.Log("Process ShotEffects");
for (int i = 0; i < laserGuns.Length; i++)
{
laserGuns[i].SetShotEffects();
GameObject go = Instantiate(laserGuns[i].ammoPrefab, laserGuns[i].transform.position, laserGuns[i].transform.rotation) as GameObject;
DestroyAmmo d = go.GetComponent<DestroyAmmo>();
d.playerThatShot = ammo$$anonymous$$anager;
}
// ammo$$anonymous$$anager.laserInUse.UseAmmo(laserAmount);
}
[Command]
void CmdProcessRocketEffect()
{
RpcProcessRocketEffect();
}
[ClientRpc]
void RpcProcessRocketEffect()
{
if (Time.time > nextRocketFire)
{
rocketLauncher.SetShotEffects();
Debug.Log("C$$anonymous$$D+RPC Rocket");
nextRocketFire = Time.time + rocketLauncher.fireRate;
GameObject instance = Instantiate(rocketLauncher.ammoPrefab, rocketLauncher.transform.position, rocketLauncher.transform.rotation) as GameObject;
DestroyAmmo d = instance.GetComponent<DestroyAmmo>();
d.playerThatShot = ammo$$anonymous$$anager;
}
}
You can get around with this. Hope you find something. Good luck!