Remote clients can't spawn objects - Unity mirror networking
I'm trying to make an online third person shooter, so far I managed to get it working, but for some reason I can't spawn bullets from any remote client but I can from the host. It seems that either the command is not being called or that it just won't spawn. I have a lobby system, so when all player are ready it just changes the scene and spawn the corresponding PlayerObject with authority, all shooting mechanic scripts are in this GameObject. I won't include the lobby code since I think it won't affect anything, if you need it feel free to ask. Client authority is ticket.
The issue: It will only spawn if the one shooting is the host and everyone will see it properly, but if the one who shoots is a remote client it won't spawn for anybody, not even for the remove client itself. It's like if I couldn't send CMDs from remote clients.
Update: I tried calling Instantiate() locally and then using the CMD to say NetworkServer.Spawn(), and it worked perfectly for the host (just like before), but it didn't for the remote clients, they see the local Instance but the CMD doesn't seem to spawn anything in the server.
Here's the code:
This method is called every frame you fire from LateUpdate, fireRate is handeled previously
private void FireBullet()
{
if (ammoCount <= 0) return;
ammoCount--;
foreach (var particle in muzzleFlash)
{
particle.Emit(1);
}
activeWeapon.SpawnBullet(bulletPrefab, bulletOrigin, bulletSpeed);
}
This is executed inside the player
private Transform bulletOrigin;
private GameObject bulletPrefab;
public void SpawnBullet(GameObject bulletPrefab, Transform bulletOrigin, float speed)
{
this.bulletOrigin = bulletOrigin;
this.bulletPrefab = bulletPrefab;
Debug.Log("Spawning");
CmdCreateBullet(speed);
}
[Command]
void CmdCreateBullet(float speed)
{
GameObject bullet = Instantiate(bulletPrefab, bulletOrigin.position, bulletOrigin.rotation);
bullet.transform.LookAt(lookAtTransform);
bullet.GetComponent<Rigidbody>().AddForce(bullet.transform.forward * speed, ForceMode.Impulse);
NetworkServer.Spawn(bullet, connectionToClient);
}
Answer by JoaquinSosa · Nov 09, 2020 at 05:53 PM
The issue was that I was trying to use variables in the server that I had only defined locally, there's no error message for this but it won't work properly. What I ended up doing was first creating an enum with the types of bullets so I don't have to pass the prefab as a parameter, if it's a prefab and we are using C# you can cast a byte (or int) into an enum (you're sending way less information to get the same result), and since you're spawning a bullet everytime you shoot is an excellent practice, every time I shoot the server gets a call (CMD), in the cmd I have the bullet type, bullet origin, bullet direction and force, since all that information is passed as parameters the server doesn't use any local variable. In the CMD I use said information to spawn the bullet in the server since we only want to detect server-sided bullet collisions but have a visual of it in every client, that's when RPC comes into the game. After spawning the bullet in the server I call a RPC which will spawn the bullet in every client except from the host, since the host is the server, the bullet has already been spawned, the bullet spawned in every client is only used as a visual, not an actual bullet which has collisions, this is due to the delay between players, one player could see a collision while someother doesn't. Managing collisions in the server is the best practice; to only detect collisions from the bullet spawned in the server and ignore the ones spawned in the clients I use NetworkServer.isActive, which will return true if that object is active only in the server, after overwriting OnCollisionEnter I check if NetworkServer.isActive, if true then proceed with the collision management, otherwise just return. When hitting players and taking damage is easy since you always return if you are not the server all the things you do when a bullet hits something is done in the server, that's why you can call damage methods which are supposed to be server sided. This is the whole explanation of how I ended up addressing the shooting mechanism with Mirror, the base solution to the question above is just about variable managment, where I was trying to access data from the server without it being defined in it. In case anyone needs some code example just message me.