Resolved the obstacle spawning issue

This commit is contained in:
Michal Pikulski
2025-09-18 15:51:18 +02:00
parent c5ce2ae1af
commit 9be8d1619b
4 changed files with 185 additions and 56 deletions

View File

@@ -155,18 +155,21 @@ namespace Minigames.DivingForPictures
/// </summary>
private void CheckIfOffScreen()
{
if (_mainCamera == null) return;
// Calculate screen top if not cached
if (_screenTop == 0f)
if (_mainCamera == null)
{
Vector3 topWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 1f, _mainCamera.nearClipPlane));
_screenTop = topWorldPoint.y;
_mainCamera = Camera.main;
if (_mainCamera == null) return;
}
// Check if obstacle is above screen
if (transform.position.y > _screenTop + 2f) // Extra buffer for safety
// Always recalculate screen bounds to ensure accuracy
Vector3 topWorldPoint = _mainCamera.ViewportToWorldPoint(new Vector3(0.5f, 1f, _mainCamera.transform.position.z));
_screenTop = topWorldPoint.y;
// Check if obstacle is significantly above screen top (obstacles move upward)
// Use a larger buffer to ensure obstacles are truly off-screen before returning to pool
if (transform.position.y > _screenTop + 5f)
{
Debug.Log($"[FloatingObstacle] {gameObject.name} off-screen at Y:{transform.position.y:F2}, screen top:{_screenTop:F2}");
ReturnToPool();
}
}
@@ -176,14 +179,33 @@ namespace Minigames.DivingForPictures
/// </summary>
private void ReturnToPool()
{
// CRITICAL: Stop all behavior first to prevent race conditions
// This ensures no more off-screen checks or movement happen during pool return
StopObstacleBehavior();
if (spawner != null)
{
spawner.ReturnObstacleToPool(gameObject, prefabIndex);
}
else
{
Debug.LogWarning($"[FloatingObstacle] Cannot return {gameObject.name} to pool - missing spawner reference");
Destroy(gameObject);
// Try to find the spawner instead of destroying the object
ObstacleSpawner foundSpawner = FindFirstObjectByType<ObstacleSpawner>();
if (foundSpawner != null)
{
Debug.LogWarning($"[FloatingObstacle] Obstacle {gameObject.name} lost spawner reference, found replacement spawner");
spawner = foundSpawner;
spawner.ReturnObstacleToPool(gameObject, prefabIndex);
}
else
{
// No spawner found - just deactivate the object instead of destroying it
Debug.LogWarning($"[FloatingObstacle] No spawner found for {gameObject.name}, deactivating safely");
gameObject.SetActive(false);
// Move to a safe location to avoid interference
transform.position = new Vector3(1000f, 1000f, 0f);
}
}
}
@@ -201,7 +223,9 @@ namespace Minigames.DivingForPictures
/// </summary>
public void OnSpawn()
{
// Reset all state first
_screenTop = 0f; // Reset cached screen bounds
_mainCamera = Camera.main; // Refresh camera reference
// Re-enable the collider for reuse
if (_collider != null)
@@ -209,10 +233,9 @@ namespace Minigames.DivingForPictures
_collider.enabled = true;
}
// Ensure the obstacle is active and visible
gameObject.SetActive(true);
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} spawned from pool");
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} spawned");
// Note: Don't start coroutines here - OnEnable() will handle that when SetActive(true) is called
}
/// <summary>
@@ -220,13 +243,16 @@ namespace Minigames.DivingForPictures
/// </summary>
public void OnDespawn()
{
// Stop all coroutines before returning to pool
StopObstacleBehavior();
// Re-enable collider for next use (in case it was disabled)
if (_collider != null)
{
_collider.enabled = true;
}
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} despawned");
Debug.Log($"[FloatingObstacle] Obstacle {gameObject.name} despawned to pool");
}
/// <summary>

View File

@@ -21,6 +21,7 @@ namespace Minigames.DivingForPictures
FloatingObstacle obstacleComponent = obstacle.GetComponent<FloatingObstacle>();
if (obstacleComponent != null)
{
Debug.Log($"[ObstaclePool] Returning obstacle {obstacle.name} to pool");
Return(obstacleComponent, prefabIndex);
}
else
@@ -37,7 +38,16 @@ namespace Minigames.DivingForPictures
/// <returns>An obstacle instance ready to use</returns>
public GameObject GetObstacle(int prefabIndex)
{
Debug.Log($"[ObstaclePool] GetObstacle called for prefab index {prefabIndex}");
FloatingObstacle obstacleComponent = Get(prefabIndex);
if (obstacleComponent == null)
{
Debug.LogError($"[ObstaclePool] Get() returned null for prefab index {prefabIndex}");
return null;
}
Debug.Log($"[ObstaclePool] Get() returned obstacle {obstacleComponent.name}, active state: {obstacleComponent.gameObject.activeInHierarchy}");
return obstacleComponent.gameObject;
}
}

View File

@@ -74,6 +74,7 @@ namespace Minigames.DivingForPictures
private float _spawnRangeX;
private Coroutine _spawnCoroutine;
private readonly List<GameObject> _activeObstacles = new List<GameObject>();
private int _obstacleCounter = 0; // Counter for unique obstacle naming
private void Awake()
{
@@ -169,9 +170,6 @@ namespace Minigames.DivingForPictures
// Initialize the pool
_obstaclePool.Initialize(prefabObstacles);
// Periodically trim the pool
InvokeRepeating(nameof(TrimExcessPooledObstacles), 15f, 30f);
}
/// <summary>
@@ -252,6 +250,8 @@ namespace Minigames.DivingForPictures
/// </summary>
private void TrySpawnObstacle()
{
Debug.Log($"[ObstacleSpawner] TrySpawnObstacle called at {Time.time:F2}");
if (obstaclePrefabs == null || obstaclePrefabs.Count == 0)
{
Debug.LogWarning("[ObstacleSpawner] No obstacle prefabs available for spawning!");
@@ -268,15 +268,20 @@ namespace Minigames.DivingForPictures
if (IsValidSpawnPosition(spawnPosition))
{
Debug.Log($"[ObstacleSpawner] Found valid position at {spawnPosition} after {attempts + 1} attempts");
SpawnObstacleAt(spawnPosition);
foundValidPosition = true;
break;
}
else
{
Debug.Log($"[ObstacleSpawner] Position {spawnPosition} invalid (attempt {attempts + 1}/{maxSpawnAttempts})");
}
}
if (!foundValidPosition)
{
Debug.Log($"[ObstacleSpawner] Could not find valid spawn position after {maxSpawnAttempts} attempts");
Debug.LogWarning($"[ObstacleSpawner] SPAWN MISSED: Could not find valid spawn position after {maxSpawnAttempts} attempts at {Time.time:F2}");
}
}
@@ -308,13 +313,15 @@ namespace Minigames.DivingForPictures
/// </summary>
private void SpawnObstacleAt(Vector3 position)
{
Debug.Log($"[ObstacleSpawner] SpawnObstacleAt called for position {position}");
// Select random prefab
int prefabIndex = Random.Range(0, obstaclePrefabs.Count);
GameObject prefab = obstaclePrefabs[prefabIndex];
if (prefab == null)
{
Debug.LogError($"[ObstacleSpawner] Obstacle prefab at index {prefabIndex} is null!");
Debug.LogError($"[ObstacleSpawner] SPAWN FAILED: Obstacle prefab at index {prefabIndex} is null!");
return;
}
@@ -323,32 +330,53 @@ namespace Minigames.DivingForPictures
// Spawn using pool or instantiate directly
if (useObjectPooling && _obstaclePool != null)
{
Debug.Log($"[ObstacleSpawner] Requesting obstacle from pool (prefab index {prefabIndex})");
obstacle = _obstaclePool.GetObstacle(prefabIndex);
if (obstacle == null)
{
Debug.LogError("[ObstacleSpawner] Failed to get obstacle from pool!");
Debug.LogError($"[ObstacleSpawner] SPAWN FAILED: Failed to get obstacle from pool for prefab index {prefabIndex}!");
return;
}
Debug.Log($"[ObstacleSpawner] Got obstacle {obstacle.name} from pool, active state: {obstacle.activeInHierarchy}");
// FORCE ACTIVATION - bypass pool issues
if (!obstacle.activeInHierarchy)
{
Debug.LogWarning($"[ObstacleSpawner] Pool returned inactive object {obstacle.name}, force activating!");
obstacle.SetActive(true);
Debug.Log($"[ObstacleSpawner] After force activation, {obstacle.name} active state: {obstacle.activeInHierarchy}");
}
obstacle.transform.position = position;
obstacle.transform.rotation = prefab.transform.rotation;
obstacle.transform.SetParent(transform);
Debug.Log($"[ObstacleSpawner] After positioning, obstacle {obstacle.name} active state: {obstacle.activeInHierarchy}");
}
else
{
Debug.Log($"[ObstacleSpawner] Instantiating new obstacle (pooling disabled)");
obstacle = Instantiate(prefab, position, prefab.transform.rotation, transform);
}
// Assign unique name with counter
_obstacleCounter++;
string oldName = obstacle.name;
obstacle.name = $"Obstacle{_obstacleCounter:D3}";
Debug.Log($"[ObstacleSpawner] Renamed obstacle from '{oldName}' to '{obstacle.name}', active state: {obstacle.activeInHierarchy}");
// Configure the obstacle
ConfigureObstacle(obstacle, prefabIndex);
Debug.Log($"[ObstacleSpawner] After configuration, obstacle {obstacle.name} active state: {obstacle.activeInHierarchy}");
// Track active obstacles
_activeObstacles.Add(obstacle);
// Invoke events
onObstacleSpawned?.Invoke(obstacle);
Debug.Log($"[ObstacleSpawner] After events, obstacle {obstacle.name} active state: {obstacle.activeInHierarchy}");
Debug.Log($"[ObstacleSpawner] Spawned obstacle {obstacle.name} at {position}");
Debug.Log($"[ObstacleSpawner] Successfully spawned obstacle {obstacle.name} at {position}. Active count: {_activeObstacles.Count}, Final active state: {obstacle.activeInHierarchy}");
}
/// <summary>
@@ -377,6 +405,8 @@ namespace Minigames.DivingForPictures
{
if (obstacle == null) return;
Debug.Log($"[ObstacleSpawner] ReturnObstacleToPool called for {obstacle.name}, active state: {obstacle.activeInHierarchy}");
// Remove from active list
_activeObstacles.Remove(obstacle);
@@ -386,25 +416,16 @@ namespace Minigames.DivingForPictures
// Return to pool or destroy
if (useObjectPooling && _obstaclePool != null)
{
Debug.Log($"[ObstacleSpawner] Returning {obstacle.name} to pool");
_obstaclePool.ReturnObstacle(obstacle, prefabIndex);
}
else
{
Debug.Log($"[ObstacleSpawner] Destroying {obstacle.name} (pooling disabled)");
Destroy(obstacle);
}
}
/// <summary>
/// Called periodically to trim excess pooled obstacles
/// </summary>
private void TrimExcessPooledObstacles()
{
if (_obstaclePool != null)
{
_obstaclePool.TrimExcess();
}
}
/// <summary>
/// Public method to change spawn interval at runtime
/// </summary>