Search Unity

Question How to let the host see different things ingame (making clients spectators only)

Discussion in 'Multiplayer' started by MaddinOriginal, Nov 8, 2022.

  1. MaddinOriginal

    MaddinOriginal

    Joined:
    Nov 8, 2022
    Posts:
    2
    So I am making a quiz kind of game. I want the clients to spectate and NOT see what the host sees. The host for example should have a screen where they can type a question and a correct answer and also some wrong answers. After they are done, the clients that are spectating on another screen/canvas/scene should get the question and the answers and can then decide which answer they think is correct.

    This would obviously not work if the clients see the same screen as the host and see what the host is typing in or even worse what answer the host defines as the correct answer. But after googling for a while I only found tutorials in which Host and Client(s) see the exact same things ingame. Probably because in most games you want them to be in the same world, seeing the same things. But I dont. And it is frustrating that it seems there is no way to do this...

    I also tried some things inside Unity but it never worked like I wanted it to. Which is why Im here asking you kind souls to hopefully help me out on what I can try to achieve this.

    Thanks in advance.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,892
    This isn‘t too hard actually. First, consider that the UI should not be networked at all. Simply instantiate local prefabs for the UI of each client and a different for the host. Now when the host enters a question, you‘ll make a „send“ button that calls a ClientRpc with the question string as a parameter. You can use a non visible (empty) networked game object as the sender receiver. On the client side this object updates the client UI whenever a ClientRpc is received. It‘s as simple as that.
     
  3. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    You did not mention what network solution you're using, but this is done pretty easily on Fish-Networking.

    There's a variety of ways it could be done but the first that comes to mind is the Match Condition. https://fish-networking.gitbook.io/docs/manual/components/network-observer

    Ultimately all you would do is instantiate your object normally. Then do MatchCondition.AddToMatch(clientHost) and network spawn the object. Now only host can see the object. When you want to add rest of the players you can also add them to the match the same way. Players can be removed from matches as well.

    The match condition isn't too difficult to use but is the most flexible one so if you have any questions feel free to ask.

    Writing your own condition is an easy option too and could be a lot easier to use. Let me know.
     
  4. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    I actually went ahead and made a custom condition to show you what it would look like. There's also a feature coming in the next release where you can toggle the state on conditions, which would allow you to deactive this condition for all clients to see the object.

    I know you haven't announced your networking solution yet but your question gave me some creative insight into a new feature and I wanted to share the results on how it could potentially help.
    Code (CSharp):
    1.     [CreateAssetMenu(menuName = "FishNet/Observers/Host Only Condition", fileName = "New Host Only Condition")]
    2.     public class HostOnlyCondition : ObserverCondition
    3.     {
    4.         public override bool ConditionMet(NetworkConnection connection, bool currentlyAdded, out bool notProcessed)
    5.         {
    6.             notProcessed = false;
    7.             /* Only return true if connection is the local client.
    8.              * This check only runs on the server, so if local client
    9.              * is true then they must also be the server (clientHost). */
    10.             return (base.NetworkObject.ClientManager.Connection == connection);
    11.         }
    12.  
    13.         public override bool Timed()
    14.         {
    15.             return false;
    16.         }
    17.  
    18.         public override ObserverCondition Clone()
    19.         {
    20.             HostOnlyCondition copy = ScriptableObject.CreateInstance<HostOnlyCondition>();
    21.             return copy;
    22.         }
    23.     }
     
  5. MaddinOriginal

    MaddinOriginal

    Joined:
    Nov 8, 2022
    Posts:
    2
    Thank you so much for your answers @CodeSmile and @Punfish

    To clarify, I am using Netcode because if I read correctly it is by Unity itself so probably has the best support and integration. But currently I have a lot of issues with it so I might try out Fish-Networking if it actually is that much easier. Also I think I abandon my project for now and create a new one just to learn how to properly use Multiplayer in Unity before I get back to the quiz game.

    Again, thank you for your answers. I will try them out :)
     
    Punfish likes this.