Network - Firing Projectile RPC Issue?

Hello,

I’m trying to get my projectile to fire on my network game (being a client or server). And I don’t actually know the best way to do this, I don’t think I’m doing it correct at all.

Whats happening right now, is if I fire on the server, my character fires fine but the clients also fire, but in an upward direction (its top down). If I fire on the clients side, you see the projectile fire out of everyones gun for a split second, then it disappears.

I’m trying to get to grips with the RPC, I think I see one of my problem, but again, I don’t even know if this is the correct way to do it.

This is my gun script:

#pragma strict

var muzzleShot : Transform;
var paintballRound : Rigidbody;

function Update () {
	if(GameObject.FindWithTag("Player")){
		
		if (Input.GetButtonDown("Fire1")) {
			
			if(networkView.isMine){
				Fire();
			}
			else{
				var viewID = Network.AllocateViewID();
				networkView.RPC("SendFire", RPCMode.AllBuffered, viewID, muzzleShot.transform.position, muzzleShot.transform.rotation); //viewID
			}
		}
    }
}

function Fire(){
	var clone : Rigidbody;
	clone = Instantiate(paintballRound, muzzleShot.transform.position, muzzleShot.transform.rotation);
	//clone = Network.Instantiate(paintballRound, muzzleShot.transform.position, muzzleShot.transform.rotation, 0);
        	
	Physics.IgnoreCollision(clone.collider, GameObject.FindWithTag("Player").collider);
	clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}


@RPC
function SendFire(viewID : NetworkViewID, location : Vector3, rotate : Quaternion){	
		
	var clone : Transform;
	clone = Instantiate(paintballRound, location, rotate.identity) as Transform;
	var nView : NetworkView;
	nView = clone.GetComponent(NetworkView);
	nView.viewID = viewID;
}

So whats it saying it, if the network is mine (I’m the player), fire a normal projectile NOT using the network, else if you are a client (viewing the person shooting), then also fire a projectile < this is why I believe the projectiles fire upwards on the client, because it isn’t spawning it at the tip of the players gun, nor shooting in the right direction.

Please help me out, I’ve been stuck on this for days. Once I get this, I’ll be able to get on my way!

Thanks all.

The way that works at the moment, all of the other players fire the RPC!!

You want to put that SendFire code only in the networkView.isMine call - that way the player actually doing the firing then goes and tells the others to fire.

You also don’t want to use AllBuffered as that also tells you to instantiate again. Try OthersBuffered if you also want to do your Fire() code.

So your fire code could look like this:

function Update () {
   if(networkView.isMine)
   {

       if (Input.GetButtonDown("Fire1")) {

          Fire();
          var viewID = Network.AllocateViewID();
          networkView.RPC("SendFire", RPCMode.OthersBuffered, viewID, muzzleShot.transform.position, muzzleShot.transform.rotation); //viewID
         }
       
    }
}

Update:

Oliver also made his bullets not use the networkViewID and had his code move them on the client independently - my guess is that both the networkView and the local bullet code were moving the same item causing problems.