What do you know about network instantiation? It’s not quite as straightforward as you think. Try turning the ‘ShootMissiles’ function into an RPC- split it up into two functions, like this-
function ShootMissiles(){
var raymissiles : Ray = camera.main.ScreenPointToRay(Vector3(Screen.width/2,Screen.height/2,0));
for (var i in spawnMissili){
yield WaitForSeconds(0.3);
if(networkView.isMine)
{
// This bit tells the network to set up a viewID for you to assign manually.
var missileViewID : NetworkViewID = Network.AllocateViewID();
// Here we distribute the signal to all players, with the viewID to sync the missile!
networkView.RPC("SpawnMissile", RPCMode.All, i.position, i.localRotation, transform.forward*250);
}
}
}
@RPC
function SpawnMissile(position : Vector3, rotation : Quaternion, force : Vector3, viewID : NetworkViewID)
{
var missile = Instantiate(rocket, position, rotation);
missile.transform.localEulerAngles = transform.localEulerAngles;
//missile.transform.Rotate(Vector3.up*90, Space.Self);
missile.rigidbody.AddForce(force, ForceMode.Impulse);
missile.networkView.viewID = viewID;
//Physics.IgnoreCollision(missile.collider, collider);
}
Now, having done this, we then need to add a few things to the missile to make it synchronise between players properly (since because the physics engine is somewhat unstable, you won’t be guaranteed that it’ll land at the same spot on every machine, especially at long range). To do this, we will make the player that fired the missile ‘authoritative’, and adjust the missile’s flight-path to their local physics simulation if it deviates on other computers.
First, make sure that your missile prefab has a networkView component, then add a script that goes something like this-
var remoteVelocity : Vector3;
var remoteLocation : Vector3;
var remoteRotation : Quaternion;
function FixedUpdate()
{
if(networkView.isMine)
{
remoteVelocity = rigidbody.velocity;
remoteLocation = rigidbody.position;
remoteRotation = rigidbody.rotation;
} else {
rigidbody.velocity = remoteVelocity;
rigidbody.position = remoteLocation;
rigidbody.rotation = remoteRotation;
}
}
function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
stream.Serialize(remoteVelocity);
stream.Serialize(remoteLocation);
stream.Serialize(remoteRotation);
}
Then, make sure that this script is the ‘observed’ value on the missile’s networkView, and that the networkView is using ‘Unreliable’ synchronisation mode (since we expect the values to change every frame).