Hello,
Everywhere I have researched states PhotonNetwork.Instantiate() should only be used for major player controlled items such as avatars, and that bullets and attacks, etc. should be made using RPC calls for efficiency.
I tested both ways, and for some reason the RPC method is much slower performance wise, and has lag and framerate impacts for high speed projectile creation, such as machine guns.
Below are the two pieces of code I tested for firing bullets.
RPC Method Call:
[PunRPC]
void FireBulletViaRPC(int ownerPVID, Vector3 bulletLoc, Quaternion bulletRotation, Vector3 ownerVelocity) {
//Right way, inside an RPC call
GameObject tmp = Instantiate (bulletPrefab, bulletLoc, bulletRotation);
//We need to add our velocity to bullet so we dont fly past it at full speed.
//Connect with bullet first to tel it about creator
MultiplayerRPCBullet tmpBlt = tmp.GetComponent<MultiplayerRPCBullet> ();
tmpBlt.ownerPVID = ownerPVID; //who's yur daddy
tmpBlt.myOwnerVelocity = ownerVelocity; //start at his speed
}
And I compare that to my original code:
void FireBulletViaInstantiate(int ownerPVID, Vector3 bulletLoc, Quaternion bulletRotation, Vector3 ownerVelocity) {
GameObject tmp = PhotonNetwork.Instantiate (pathToBullet, bulletLoc, bulletRotation, 0);
//We need to add our velocity to bullet so we dont fly past it at full speed.
//Connect with bullet first to tel it about creator
MultiplayerRPCBullet tmpBlt = tmp.GetComponent<MultiplayerRPCBullet> ();
tmpBlt.ownerPVID = ownerPVID; //who's yur daddy
tmpBlt.myOwnerVelocity = ownerVelocity; // start at his speed
}
For both methods they are simply called with mouse0 down event handler with a cooldown, and encapuslated with PV.ismine properly. Since that code is shared by both tests, I felt it is not relevant to share it.
There is significant performance increase of after effects, such as bullet impacts etc. showing up much more reliably using RPC method calls. With this method all projectiles fired are visible, never lost, and always produce their destruction effects, but collision with players is very unreliable.
However, using the network instatiate produces much better collision detection, at the cost of having more complications when deleting a network instantiate projectile with a PV component.
I have seen other people new to network programming state they used PhotonNetwork.Instantiate() within the RPC method. That makes no sense to me, as you would be broadcasting a messgae to all players, and they in turn would broadcast a room-wide instantiation, causing a cascade of more projectiles than necessary, is that incorrect?
I though the whole purpose of using RPC method, was each machine would just locally create their own projectiles, which have no PhotonView component and just operate locally.
If anyone can clarify what I am doing wrong, it would be appreciated, thanks.