I’m currently working on an FPS and I’m trying to network a bullet impact.
I’ve coded it in such a way that the bullet impact prefab is instantiated on the local client so the client can see where it has shot, which then sends a command to spawn the bullet impact prefab over the network at the same location as the client.
Here’s my code:
// Hit is from a raycast
GameObject impact = Instantiate(impactEffect, hit.point, Quaternion.identity);
Destroy(impact, 1.5f);
// Prevents double-instantiation on LAN games
if (!isServer) {
// If you're not the host of the game, send the impact location to the host
CmdSendServerImpactEffectLocation(hit.point);
} else {
// If you are the host of the game, send the impact location to the clients
RpcSendClientImpactEffectLocation(hit.point);
}
[Command]
void CmdSendServerImpactEffectLocation(Vector3 loc) {
GameObject impact = Instantiate(impactEffect, loc, Quaternion.identity);
Destroy(impact, 1.5f);
NetworkServer.Spawn(impact);
}
[ClientRpc]
void RpcSendClientImpactEffectLocation(Vector3 loc) {
GameObject impact = Instantiate(impactEffect, loc, Quaternion.identity);
Destroy(impact, 1.5f);
}
The first part is wrapped in a method, obviously. What I have done here, unfortunately, doesn’t work on matchmaking. Since there is no player that is the host/server, the client sees the instantiation happen twice, one locally, and one from the Network.Spawn()
Any suggestions/information about anything relating to my issue is greatly appreciated! Thank you so much! I’m also wondering, is the user that created the matchmaking room considered the host or the server?