RisottoMan / AutoEvent

MIT License
31 stars 20 forks source link

Error in the mini-game Musical Chairs #108

Open RisottoMan opened 1 day ago

RisottoMan commented 1 day ago

While the mini-game is running on a large number of servers and on a large number of players, the game may freeze and not end.

RisottoMan commented 1 day ago

Video: https://github.com/user-attachments/assets/cc199aba-3b89-44ce-a69d-48cfbd57c581

RisottoMan commented 2 hours ago

Try to use raycast instead of using 2 foreach for players and platforms:


var tempDict = new List<KeyValuePair<Player, GameObject>>();
var occupiedPlatforms = new HashSet<GameObject>();
var players = Player.GetPlayers();

foreach (var player in players)
{
    var playerPosition = player.Position;

    if (Physics.Raycast(playerPosition, Vector3.down, out RaycastHit hit, Mathf.Infinity))
    {
        var platform = hit.collider.gameObject;

        if (Platforms.Contains(platform) && !occupiedPlatforms.Contains(platform))
        {
            platform.GetComponent<PrimitiveObjectToy>().NetworkMaterialColor = Color.red;
            occupiedPlatforms.Add(platform);
            tempDict.Add(new KeyValuePair<Player, GameObject>(player, platform));
        }
    }
}