An Issue With PhotonNetwork.Instantiate

I'm having some trouble with instantiating a bullet. Say I have two clients, Client X and Client Y, if Client X fires (using PhotonNetwork.Instantiate) they will see it, but Client Y will not see it. What I want it to do is obvious, if Client X fires, both clients will see it.

Here is the code for the firing script:
using UnityEngine;
using System.Collections;

public class MachineGun2 : MonoBehaviour {
	public bool multipleGuns = false;
	public int shotQue = 0;
	public bool isOnline = false;
	public float rateOfFire = 0.1f;
	public GameObject bullet;
	public GameObject networkBullet;
	public float power = 15000;
	public PhotonView thisPhotonView;
	// Use this for initialization
	void Start () {
		thisPhotonView = gameObject.GetComponent<PhotonView> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButtonDown ("Fire1") && thisPhotonView.isMine) {
			InvokeRepeating("Shoot", .001f, rateOfFire);
		}
		if (Input.GetButtonUp ("Fire1") && thisPhotonView.isMine) {
			CancelInvoke("Shoot");
		}
	}

	void Shoot () {
		if (!isOnline) {
			if (shotQue == 0) {
				GameObject instance;
				instance = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
				Vector3 fwd = transform.TransformDirection(Vector3.back);
				instance.rigidbody.AddForce(fwd * power * 10);
				audio.Play();
			}
		}else if (isOnline) {
			if ((shotQue == 0)) {
				GameObject instance;
				instance = (GameObject)PhotonNetwork.Instantiate("ChaingunShot", transform.position, transform.rotation, 0);
				Vector3 fwd = transform.TransformDirection(Vector3.back);
				instance.rigidbody.AddForce(fwd * power * 10);
				audio.Play();
			}
		}

		if (multipleGuns) {
			if (shotQue == 0) {
				shotQue += 1;
			} else if (shotQue == 1) {
				shotQue -= 1;
			}
		}
	}
}

In the code, under the Update function, I have it check if it controls the PhotonView (PhotonView.isMine) so that if Client X fires, he doesn't see the gameObject representing Client Y also fire on his screen after picking up the input (and, again, Client Y will see none of it). If I remove that then all gameobjects in the scene with that firing script all fire simultaneously, but only for the Client who fired (by this I mean only the person who fires sees the other players firing on his screen).

So, I need help in making sure that when one player fires that only that person fires, and that all other clients can see that person fire.

Thanks for any help.

Comments

  • Use RPC's for sending shooting events across instances of player on clients. They can handle bullet objects in these RPC calls.
    You never need PhotonNetwork.Instantiate for objects like bullets. It's too expensive. Instead, send shot parameters and visualize bullets locally basing on this parameters. Same for hits. Detect them on shooting client and notify others with RPC.