- Add a method to shoot, and synchronize it across the network using
ServerRpc:
public class PlayerController : NetworkBehaviour
{
public GameObject bulletPrefab;
public Transform bulletSpawn;
void Update()
{
if (!IsOwner) return;
// Movement code...
if (Input.GetButtonDown("Fire1"))
{
ShootServerRpc();
}
}
[ServerRpc]
void ShootServerRpc()
{
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
bullet.GetComponent<NetworkObject>().Spawn();
}
}