I’m putting together a Multiplayer Shooter and for some reason, when I call the function to shoot a bullet, it works with the host player, but not the client player. Here is the function in question
[Command]
void CmdFire()
{
ammoInClip--;
updateAmmoCounter();
Vector3 pos = cameraObject.transform.position;
Vector3 rot = transform.eulerAngles;
float drift = 0.002f * (100f - weaponRef.weaponList[0].getAccuracy());
Vector3 aimDrift = new Vector3(Random.Range(-drift, drift), Random.Range(-drift, drift), 0f);
GameObject newBullet = Instantiate(
bulletObject,
pos,
Quaternion.Euler(rot));
NetworkServer.Spawn(newBullet);
//newBullet.transform.rotation(Quaternion.Euler(aimDrift));
}
And here is the rest of the script (Pardon the mess)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
public class Player_GunController : NetworkBehaviour, IPointerDownHandler
{
public Animation augCon;
public GameObject shootPoint;
public ParticleSystem shootParticles;
private Weapons weaponRef;
public Animation gunAnim;
public WeaponAnimClips animClips;
public GameObject cameraObject;
public GameObject hitTest;
public GameObject bulletObject;
public Text ammoCounter;
private int ammoInClip;
bool fire,startReload;
float fireRate;
public Image fireButton;
void Start()
{
if (!isLocalPlayer && !GetComponent<Player_Movement>().isSinglePlayer)
{
Destroy(this);
}
weaponRef = Weapons.Instance;
// shootParticles = shootPoint.GetComponentInChildren<ParticleSystem>();
gunAnim = GetComponentInChildren<Animation>();
animClips = GetComponentInChildren<WeaponAnimClips>();
// gunAnim.Play(animClips.clips[0].name);
ammoInClip = weaponRef.weaponList[0].getClipSize();
updateAmmoCounter();
}
void Update()
{
if (isLocalPlayer || GetComponent<Player_Movement>().isSinglePlayer)
{
if (Input.GetAxis("Fire1") > 0 && !startReload)
{
if (fireRate <= 0)
{
// gunAnim.Rewind();
// gunAnim.Play(animClips.clips[Random.Range(3, 5)].name);
if (ammoInClip <= 0 && !startReload)
{
fire = false;
startReload = true;
reloadGun();
}
else if (!startReload)
{
CmdFire();
}
fireRate = weaponRef.weaponList[0].getFireRate();
}
else
{
fireRate -= Time.deltaTime;
}
}
if (gunAnim != null && !gunAnim.IsPlaying(animClips.clips[2].name) && startReload)
{
ammoInClip = weaponRef.weaponList[0].getClipSize();
updateAmmoCounter();
startReload = false;
}
}
}
public void reloadGun()
{
// gunAnim.Play(animClips.clips[2].name);
startReload = true;
}
[Command]
void CmdFire()
{
ammoInClip--;
updateAmmoCounter();
Vector3 pos = cameraObject.transform.position;
Vector3 rot = transform.eulerAngles;
float drift = 0.002f * (100f - weaponRef.weaponList[0].getAccuracy());
Vector3 aimDrift = new Vector3(Random.Range(-drift, drift), Random.Range(-drift, drift), 0f);
GameObject newBullet = Instantiate(
bulletObject,
pos,
Quaternion.Euler(rot));
NetworkServer.Spawn(newBullet);
//newBullet.transform.rotation(Quaternion.Euler(aimDrift));
}
/*
public void CmdshootHitScanner()
{
ammoInClip--;
updateAmmoCounter();
Vector3 pos = cameraObject.transform.position;
Vector3 rot = transform.eulerAngles;
Ray ray;
RaycastHit hit;
float drift = 0.002f * (100f - weaponRef.weaponList[0].getAccuracy());
Vector3 aimDrift = new Vector3(Random.Range(-drift, drift), Random.Range(-drift, drift), 0f);
GameObject newBullet = Instantiate(
bulletObject,
pos,
Quaternion.Euler(cameraObject.transform.TransformDirection(Vector3.forward) + aimDrift));
if(Physics.Raycast(pos, cameraObject.transform.TransformDirection(Vector3.forward) + aimDrift,out hit, Mathf.Infinity))
{
Instantiate(hitTest,hit.point, Quaternion.identity);
if (hit.collider.gameObject.tag == "Player")
{
// CmdShootPlayer(-5, hit.transform.gameObject);
}
}
}
*/
public void updateAmmoCounter()
{
// ammoCounter.text = ammoInClip.ToString() + "/" + weaponRef.weaponList[0].getClipSize().ToString();
}
public void OnPointerDown(PointerEventData data)
{
Debug.Log("PointerDown");
}
}