diff --git a/Assets/PerfectWorld/Scripts/Managers/EC_ManMatter.cs b/Assets/PerfectWorld/Scripts/Managers/EC_ManMatter.cs index 863fb742f4..9e9e64ff92 100644 --- a/Assets/PerfectWorld/Scripts/Managers/EC_ManMatter.cs +++ b/Assets/PerfectWorld/Scripts/Managers/EC_ManMatter.cs @@ -136,6 +136,13 @@ namespace PerfectWorld.Scripts.Managers private void SpawnMatterCube(int matterId) { + // Check if matter is within 1000 units of the host player + if (!IsMatterWithinPlayerRange(matterId, 10000f)) + { + Debug.Log($"Matter {matterId} is too far from player, skipping spawn"); + return; + } + // Find the pickupItem component in the scene and create cube for this specific matter pickupItem pickupScript = UnityEngine.Object.FindFirstObjectByType(); if (pickupScript != null) @@ -150,6 +157,13 @@ namespace PerfectWorld.Scripts.Managers private void NotifyPickupItem(int matterId) { + // Check if matter is within 1000 units of the host player + if (!IsMatterWithinPlayerRange(matterId, 1000f)) + { + Debug.Log($"Matter {matterId} is too far from player, skipping notification"); + return; + } + // Find the pickupItem component in the scene and update cubes pickupItem pickupScript = UnityEngine.Object.FindFirstObjectByType(); if (pickupScript != null) @@ -157,6 +171,43 @@ namespace PerfectWorld.Scripts.Managers pickupScript.UpdateMatterCubes(); } } + + /// + /// Check if a matter is within the specified distance from the host player + /// + /// The matter ID to check + /// Maximum distance in Unity units + /// True if matter is within range, false otherwise + private bool IsMatterWithinPlayerRange(int matterId, float maxDistance) + { + // Get the matter data + if (!matterDataStorage.TryGetValue(matterId, out info_matter matterData)) + { + Debug.LogWarning($"Matter data not found for ID: {matterId}"); + return false; + } + + // Get the host player + var hostPlayer = GameController.Instance?.GetHostPlayer(); + if (hostPlayer == null) + { + Debug.LogWarning("Host player not found"); + return false; + } + + // Convert matter position to Unity Vector3 + Vector3 matterPosition = new Vector3(matterData.pos.x, matterData.pos.y, matterData.pos.z); + + // Get player position + Vector3 playerPosition = hostPlayer.transform.position; + + // Calculate distance + float distance = Vector3.Distance(matterPosition, playerPosition); + + Debug.Log($"Matter {matterId} distance from player: {distance:F2} units (max: {maxDistance})"); + + return distance <= maxDistance; + } // Public methods for players to access matter data public info_matter? GetMatterData(int matterId)