- Home /
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.
I'm closely following the NetworkExample, so: Network.Instantiate(playerPrefab, spawnPoints.transform.position, spawnPoints.transform.rotation, 0);
Yeah sorry, I read the code again and deleted my first comment :)
Answer by whydoidoit · May 22, 2012 at 02:53 PM
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.
Okay, I tried this but now my ServerPlayer controls all the clients firing, and my client firing does fire, but only fires upward on the Z axis (i believe this is to do with the (muzzleShot. transform)
Oh hang on - authorative server? The players are sending a message to tell the server that they want to fire?
It's probably down to that GameObject.FindTag returning false on the clients? Not sure about what that's doing :)
I think you just need to use rotate, not rotate.identity
I don't mean to be troublesome, but like I said, I'm new to all this. I don't think its a authorative server, how would I know?
I changed the rotate.identity to rotate, getting there... But now if someone fires, all the players fire too?
Your answer
Follow this Question
Related Questions
RPC a lot of lags (Photon) 0 Answers
Unity3d Network - Cant See Connected Players 3 Answers
Really quick/easy question about RPCs 1 Answer
Getting rotation to work on client 1 Answer
Network RPC 1 Answer