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();
}
}
In this example, when the player presses the fire button, a ServerRpc
is called to spawn a bullet on the server, which is then synchronized with all clients.