adrenak / univoice-mirror-network

A Mirror based implementation for UniVoice voice network
MIT License
6 stars 0 forks source link

Mute them not working #5

Open anomal3 opened 2 months ago

anomal3 commented 2 months ago

I don't understand how to mute other players so they can't talk

public class VoiceChatAgent : NetworkBehaviour
{
    public short ID;

    public LayerMask LayerMask;
    public Collider[] players;

    void Start()
    {
        LayerMask = LayerMask.GetMask(new string[] { "Player" });

        InvokeRepeating(nameof(FindPlayer), 0, 5);
    }

    public override void OnStartLocalPlayer()
    {
        base.OnStartLocalPlayer();
    }

    private void FindPlayer()
    {
        if (isOwned)
        {
            CmdMute();
        }

    }

    [Command]
    void CmdMute()
    {
        players = Physics.OverlapSphere(transform.position, 100, LayerMask);

        if (players == null || players.Length <= 0)
        {
            return;
        }

        // Create a list to store the IDs of players in the radius
        HashSet<int> playersInRange = new HashSet<int>();

      // Mute players within the radius
        foreach (var p in players)
        {
            if (p != null && p.TryGetComponent(out VoiceChatAgent agent))
            {
                var peers = GameNetworkManager.singleton.agent.PeerSettings;
                if (peers.ContainsKey(agent.ID))
                {
                   // Unmute the player
                    peers[agent.ID].muteThem = false;

                    // Add player ID to the list
                    playersInRange.Add(agent.ID);
                }
            }
        }

       //Mute all other players
        var allPeers = GameNetworkManager.singleton.agent.PeerSettings;
        foreach (var peer in allPeers)
        {
            if (!playersInRange.Contains(peer.Key))
            {
                // We mute the player if he is not in the radius
                peer.Value.muteThem = true;
            }
        }
    }

What's wrong?

I'm fine getting the player ID of anyone in range, but how can I prevent him from speaking into my channel?

adrenak commented 2 months ago

Hey @anomal3 I'm not sure if I understand. You want players withing range to not be audible to you?

Currently peers[agent.ID].muteThem = false; in your first foreach loop is making them audible

anomal3 commented 2 months ago

Hey @anomal3 I'm not sure if I understand. You want players withing range to not be audible to you?

Currently peers[agent.ID].muteThem = false; in your first foreach loop is making them audible

On the contrary, so that players can be heard within the range. And those who are outside the radius were not heard. But the problem is that the player can be heard both within the range and beyond the radius

Maybe I need to run the code not on the server but on the client?

anomal3 commented 2 months ago

@adrenak help me men pls.

The essence of my business is this, I don’t want to hear players who are outside the radius. That is, I should only hear those in the circle, everyone else is mute

My Code why doesn't this code work? ```cs using Adrenak.UniVoice; using Mirror; using UnityEngine; public class VoiceChatAgent : NetworkBehaviour { [SyncVar] public short ID; public LayerMask LayerMask; public Collider[] players; void Start() { LayerMask = LayerMask.GetMask(new string[] { "Player" }); InvokeRepeating(nameof(FindPlayer), 0, 5); } private void FindPlayer() { if (isOwned) { CmdOptimizeMute(); } } [Command] void CmdOptimizeMute() { var peers = GameNetworkManager.singleton.agent.PeerSettings; // mute all players foreach (var peer in peers.Values) { //peer.muteThem = true; MuteClient(peer, true); } // We get all players within a radius of 100 units from the current position players = Physics.OverlapSphere(transform.position, 100, LayerMask); // Check if there are no players in the radius if (players == null || players.Length == 0) { return; } // Unmute players within the radius foreach (var p in players) { if (p != null && p.TryGetComponent(out VoiceChatAgent agent)) { if (peers.ContainsKey(agent.ID)) { // Unmute player //peers[agent.ID].muteThem = false; MuteClient(peers[agent.ID], false); Debug.LogWarning($""); } } } } [Client] void MuteClient(ChatroomPeerSettings peer, bool mute) { peer.muteThem = mute; } } ```
anomal3 commented 2 months ago

Perhaps a schematic drawing will explain better than me? SHEDEVR

adrenak commented 2 months ago

Hi @anomal3 let's take this to Discord and we'll fix it there. Tomorrow is a holiday so I can sit down with you