What is the best way to properly implement shooting over a network?

I know that there most likely is no universal “best way” and that it all comes down to the comfort a programmer has working with his chosen method, but I’d like to ask whether or not what I am currently doing is valid.

I have the following code for a player to control a simple player object.

using UnityEngine;
using System.Collections;

public class ShipController : MonoBehaviour
	float speed = 0.8f;
	float turnSpeed = 0.4f;
	private VariableScript ptrScriptVariable;
	void Start ()
		ptrScriptVariable = (VariableScript)GetComponent (typeof(VariableScript));
		rigidbody.useGravity = false;
	void FireRocket ()
		if (ptrScriptVariable.objBullet != null) {
			GameObject bulletClone = ptrScriptVariable.objBullet;
			Debug.Log ("Firing a rocket: " + transform.position + " , " + transform.rotation);
			Instantiate (bulletClone, transform.position + new Vector3 (0, 3, 0), transform.rotation); 
		} else {
			Debug.Log ("The bullet object is null!");
	void FixedUpdate ()
		if (networkView.isMine) {		
			//Spacebar by default will make it move forward
			if (Input.GetButton ("Jump")) { 
				rigidbody.AddRelativeForce (Vector3.forward * speed);
			// A or left arrow to turn left, D or right arrow to turn right.
			rigidbody.AddRelativeTorque (0, (Input.GetAxis ("Horizontal")) * turnSpeed, 0);
			//If the user clicks the left mouse button.
			if (Input.GetMouseButtonDown (0)) {
				networkView.RPC ("FireRocket", RPCMode.AllBuffered);
		} else {
			enabled = false;

Each of these player objects includes a NetworkView set to RDC mode, and are both spawned in through another Network Manager script. They spawn and can move together and see one another just fine. I also want to allow these players to shoot at one another, so I have a bullet prefab which I use as a projectile. Now, this prefab contains no NetworkView. Instead, I use the [RPC] code above to spawn and fire it for each player. In essence, every player is firing everyone else’s bullets too.

Is this the standard way of doing it? Is it efficient?

I’d really like to hear anyone else’s input or suggestions on this topic, thanks.

During my first foray into this problem I sync’d the positions of all bullets in my game. It was ‘accurate’ but turned horrible when there were 4+ people all shooting alot.

My next step was to do what you’ve done, RPC to send the fact that ive shot to my ‘clones’ whom im hoping are in a fairly similar position and orientation as I am. This ran much smoother but SOMETIMES there was the chance that I could shoot, hit another player and from their point of view I had missed slightly! For basics, this is pretty sufficient.

My current method is simply to send some extra information with the RPC such as my position or rotation. Depending on the circumstance, to get it pretty much spot on you wont need all XYZ rot and pos. So maybe only send XZ pos and XY rot to lighten the traffic a bit.
My clone would then shoot using the extra information I sent rather than just where they happen to be at that time.

Like other people have said, yes that is hack-able since everything is client side. An authoritative server would make sure that that doesn’t happen. I’ve never gone into anything where I expected cheating nor really bothered by it if someone tried so my 3rd iteration is whats been working well for me :slight_smile: