- Home /
Lacking Network.Instantiate Knowledge
I have a plane with a gun that shoots a bullet by instantiating a bullet prefab with a bullet physics/damage application script attached. I need this to work over the server, so should I use network.instantiate for it?
Answer by AndrewGrayGames · Feb 05, 2013 at 10:17 PM
Answer #1: Yes. You absolutely should use Network.Instantiate() from the server.
Looking forward, when your clients fire their weapons, they should use an RPC (R emote P rocedure C all, pretty much a public facing function) which talks to the server and tells it to instantiate a bullet at the muzzle's position and rotation.
You'll be getting a lot of milage out of RPCs, too; when an instantiated object moves, the server should calculate the move, and send an RPC to each of the clients to tell them to perform the move. That's a different, but related topic.
Other cool gameplay events? RPCs also.
Another thing to know: When your bullet is done, make sure you Network.Destroy() the object so that all your clients see it destroyed, again on the server.
For the movement of the bullet, shouldn't I use Network View on the bullet prefab so it can be synchronized? Or would I just instantiate it and it would go where the script would take it on all the clients?
Also, the plane is shooting quite a lot of these bullet prefabs, so would it slow down the server?
Thanks for the useful answer!
Another thing, to do that in code I would use this:
@RPC
var bullet : Transform;
function Shoot(){
Network.Instantiate(bullet, transform.position, transform.rotation, 0);
yield WaitForSeconds(.2);
}
function Update(){
if(Input.GetButton("Fire"))
{
Shoot();
}
}
Right? I'm not on a computer with Unity at the moment. I would attach this to the empty gameobject at the end of my plane's weapons.
@DynamicPrgm: Yes, make sure your moving entities have a NetworkView!
A performance impact would be deter$$anonymous$$ed by a number of factors - server RA$$anonymous$$/processor speed, bandwidth, and performance hits from scripts. It's conceivable that your 'bullet hell' plane could be firing as many as you need and only imperceptibly slow your game.
And yes, that script would work on the muzzles on your mesh! I can't test it either, but that code looks good.
Answer by lacost · Feb 05, 2013 at 10:26 PM
you don't need instantiate here, just use RPC
While the clients may not need Instantiate, the server does. In turn, the server will need to tell the clients to instantiate a local game object so everyone is on the same page. Given the OP's question involves the server, I would not say that he does not need Instantiate.
Your answer
Follow this Question
Related Questions
Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint? 1 Answer
PhotonNetwork.Instantiate 2 problems. 3 Answers
Network.Destroy and collision; destroying correct object. 1 Answer
Spawning the right prefab with Network.Instantiate 0 Answers
Checking if object intersects? 1 Answer