ObstacleSpawner 네트워크 연동

모든 클라이언트가 동일한 장애물을 보도록 함
This commit is contained in:
2026-02-02 10:08:57 +09:00
parent 10b496dfae
commit b5f8943bcc
8 changed files with 234 additions and 16 deletions

View File

@@ -1,12 +1,13 @@
using UnityEngine;
using System.Collections.Generic;
using Unity.Netcode;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Northbound
{
public class ObstacleSpawner : MonoBehaviour
public class ObstacleSpawner : NetworkBehaviour
{
[System.Serializable]
public class ObstacleEntry
@@ -89,9 +90,9 @@ namespace Northbound
Circle
}
private void Start()
public override void OnNetworkSpawn()
{
if (spawnOnStart)
if (IsServer && spawnOnStart)
{
SpawnObstacles();
}
@@ -102,6 +103,12 @@ namespace Northbound
/// </summary>
public void SpawnObstacles()
{
if (!IsServer)
{
Debug.LogWarning("[ObstacleSpawner] Only server can spawn obstacles!");
return;
}
ClearObstacles();
_spawnedPositions.Clear();
@@ -236,17 +243,12 @@ namespace Northbound
}
// 장애물 생성
Quaternion rotation = randomRotation
? Quaternion.Euler(0, Random.Range(0f, 360f), 0)
Quaternion rotation = randomRotation
? Quaternion.Euler(0, Random.Range(0f, 360f), 0)
: Quaternion.identity;
GameObject obstacle = Instantiate(obstacleEntry.prefab, randomPos, rotation);
if (groupUnderParent && _obstacleParent != null)
{
obstacle.transform.SetParent(_obstacleParent);
}
// 크기 변화
if (scaleVariation > 0)
{
@@ -254,11 +256,26 @@ namespace Northbound
obstacle.transform.localScale *= scale;
}
// Add NetworkObject component if not exists
NetworkObject networkObj = obstacle.GetComponent<NetworkObject>();
if (networkObj == null)
{
networkObj = obstacle.AddComponent<NetworkObject>();
}
if (groupUnderParent && _obstacleParent != null)
{
obstacle.transform.SetParent(_obstacleParent);
}
// Spawn on network
networkObj.Spawn();
// Add FogOfWarVisibility component to hide obstacles in unexplored areas
if (obstacle.GetComponent<FogOfWarVisibility>() == null)
{
var visibility = obstacle.AddComponent<FogOfWarVisibility>();
visibility.showInExploredAreas = false; // Obstacles hidden when not visible
visibility.showInExploredAreas = false;
visibility.updateInterval = 0.2f;
}
@@ -359,6 +376,15 @@ namespace Northbound
{
if (_obstacleParent != null)
{
foreach (Transform child in _obstacleParent)
{
NetworkObject networkObj = child.GetComponent<NetworkObject>();
if (networkObj != null && networkObj.IsSpawned)
{
networkObj.Despawn(true);
}
}
if (Application.isPlaying)
{
Destroy(_obstacleParent.gameObject);