- Home /
Answered in comments.
Network.Instantiate problem
Hello !
I have a problem with my weapons instantiation for my FPS. For each player, I instantiate my weapons and translate/rotate/change parenting them in a RPC method, and after all that I disable all of them but one. When I have only one player, no problem, and when the second one connects, there are all the weapons instantiated at the players position, but they can only be seen by the other player ! My goal is that they must not be seen at all, but must be instantiated, to make them appear some time later ...
Here is my code :
function Start () {
if (!this.transform.parent.networkView.isMine)
{
enabled = false;
return;
}
instanceCouteau = Network.Instantiate(Couteau,weaponHolder.transform.position,weaponHolder.transform.rotation,0);
instanceArbalete = Network.Instantiate(Arbalete,weaponHolder.transform.position,weaponHolder.transform.rotation,0);
networkView.RPC("InitializeWeapons", RPCMode.All);
}
@RPC
function InitializeWeapons () {
instanceCouteau.transform.parent = weaponHolder.transform;
instanceCouteau.transform.Translate(0.3116271,-0.537577,0.7420365);
instanceCouteau.transform.Rotate(-51.5672,-74.3351,30.74762);
instanceArbalete.transform.parent = weaponHolder.transform;
instanceArbalete.transform.Translate(0.2503375,-0.1833858,1.450406);
instanceArbalete.transform.Rotate(31.9898,288.8499,59.98682);
weaponHolder.transform.GetChild(0).gameObject.SetActiveRecursively(true);
weaponHolder.transform.GetChild(1).gameObject.SetActiveRecursively(false);
}
EDIT : pictures :
I tried the Start() / Awake() change, and nothing happens. Has you can see, weapons are instantiated in the Network, each player can see its weapons at its hands, but can see others players weapons and the position the player was instantiated. The client has a bunch of weapons just next to him, but can't see them (it's normal, they must be in his hand !, but only one at the time ...)
It would probably be useful to take a screenshot of the two builds (server and client1) side-by-side.
$$anonymous$$aybe if you change Start() to Awake() this will work better, since Start and Awake are two different things. I'll have to think about this more, since I'm very new to networking and still learning.
I think I know what you mean when you say the client has all of the guns (rendered and visible on screen) when they look at other players. That's because the meshRenderer is still enabled on the guns, although the Awake() fix isn't guaranteed to work.
It seems as if you're only assigning the instantiated weapons to the class fields on the machine who owns the networkView.
Try moving the entire instantiate procedure to a RPC.
That or move the initialization code to an `OnNetworkInstantiate` on the weapons themselves if possible.
I can't test it right now, but if I move my weapons instantiation in a RPC, that would mean that I call Instantiate ins$$anonymous$$d of Network.Instantiate ?
The OnNetworkInstantiate call directly on the weapons is clearly a better idea, but methods called in it won't be viewed through network, so I will still have to use a RPC call in it and it won't correct my problem, in my opinion :(
Take a look at this article about level loading over the network.
As soon as you join the server you get the RPC buffer, including instantiate calls, but then you load the level and everything's gone!
So make sure you follow the example above.