Untitled
unknown
csharp
a year ago
990 B
22
Indexable
using Unity.Netcode;
using UnityEngine;
public class ObjectSpawner : NetworkBehaviour
{
// The prefab to spawn, make sure it's registered in NetworkManager's Network Prefabs
public GameObject objectToSpawnPrefab;
// Called by the client to request the server to spawn an object
[ServerRpc]
public void RequestSpawnObjectServerRpc(Vector3 position, Quaternion rotation)
{
// Only the server should handle spawning
if (IsServer)
{
// Instantiate the object on the server
GameObject spawnedObject = Instantiate(objectToSpawnPrefab, position, rotation);
// Spawn the NetworkObject for all clients
spawnedObject.GetComponent<NetworkObject>().Spawn();
}
}
// This can be called by any client to request a spawn
[Client]
public void RequestObjectSpawn(Vector3 position, Quaternion rotation)
{
RequestSpawnObjectServerRpc(position, rotation);
}
}Editor is loading...
Leave a Comment