Search Unity

Showcase Mirror - Open Source Networking for Unity

Discussion in 'Multiplayer' started by mischa2k, Aug 11, 2016.

  1. unfocused-locust

    unfocused-locust

    Joined:
    Mar 22, 2020
    Posts:
    6
    Sorry if this is obvious, first time networking with Unity.

    I've been tinkering for a few days now with a simple turn-based, text-based game, and I've got all the specifics figured out. The only problem is that whenever I join a game as a client, it disconnects and boots me to the offline scene every time the player sends a command. This doesn't happen with a server or host connection, just the standalone client. It happens in the editor and build, with KCP or Websocket transports. I've searched just about every search term there is to search, and I can provide more details if need be. Please send help :')

    (I call the command in Update, which executes up until the command, then I get an error that whatever transport I am using got sent a "disconnect" tick.)
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    If you call it in update, do you make sure it gets called only once or are you calling it repeatedly from update?
     
  3. unfocused-locust

    unfocused-locust

    Joined:
    Mar 22, 2020
    Posts:
    6
    After some tests, it looks like it happens even if I don't call the command. My first command to add the player to the "all players" list (stored in a subclass of NetworkManager) appears to execute, but does not register server-side (the command executes in OnStartLocalPlayer). Update executes once (there are no commands called in Update), and then the client disconnects before Update can be called for the second frame.
     
  4. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    It's difficult to help you without seeing the actual code. I was just guessing that your command triggers too often from update. Since it doesn't I am out of ideas.
     
  5. unfocused-locust

    unfocused-locust

    Joined:
    Mar 22, 2020
    Posts:
    6
    Never mind! I just refactored my code after looking through some more docs. Thanks for the help, though. :D
     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Hey guys. Friendly reminder that we have Mirror Discord community with >10,000 people.
    https://discord.gg/N9QVxbM
    I usually don't have much time to reply on the forums, and MrGadget is only active in Discord :)
     
  7. alicivril

    alicivril

    Joined:
    Nov 3, 2020
    Posts:
    2
    Hi everyone,
    I have chosen Mirror for its simplicity and power for the development of a multiplayer game. One single and simple question, that I would really like to see answered: In the Network Manager component, Network Info->Transport field, I cannot see any options other than KCP. My version is 35 something, just downloaded yesterday. Any problem with this? Or, the others are not supported anymore?

    Best
     
    mischa2k likes this.
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Check out our Discord / Github page to find different transports.
    But kcp is fine :)
     
  9. alicivril

    alicivril

    Joined:
    Nov 3, 2020
    Posts:
    2
    Thanks for getting back with an answer so quick. Two more concerns/questions though: By saying kcp is fine, do you mean it is as good as to handle an action game like UDP? Second one: With KCP, can I do port forwarding on my router so that someone else can connect to my server? It seems like I can only do it with TCP or UDP from my router settings, no KCP option.
     
  10. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    KCP is a reliability algorithm. It goes over UDP. So you have to open UDP ports.
    FakeByte made a free relay for Mirror btw. Uses Epic online services. So you don't need to open ports anymore :)
     
    alicivril likes this.
  11. preynolds0521

    preynolds0521

    Joined:
    Mar 25, 2021
    Posts:
    3
    Is there a reason that an android device hosting on local wifi would not be found by another device on the same wifi trying to connect as a client? I've been searching for a while and can't find anything on android-to-android communication. I start my game up and start it as a host, which seems to work fine, but when I start it on my other phone and try to join at the same IP it tries to connect, then disconnects after a few seconds.

    Thanks!
     
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Local network connections are difficult sometimes.
    If you've ever been on a LAN party then you still remember that :)
     
  13. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    9
    In the docs, NetworkServer, as well as MsgType, are marked as deprecated, but I'm following this example to connect Playfab to Mirror.

    I checked the docs for alternative methods but couldn't find any. What should I use to replace NetworkServer and MsgType?
     
  14. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
  15. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    9
    I was looking at the correct docs though. Let me tell you exactly what I need to understand.

    I'm building a server using Unity/Playfab/Mirror. In my ServerStartUp script, I want to check the OnServerConnect so that I may invoke other functionalities. Problem is, Mirror's OnServerConnect doesn't get called when an actual user connects to playfab server. So I have to handle this manually somehow. I thought about using NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect) But this is Deprecated UNET and I can't seem to find a replacement for that.

    I ultimately just want to tell mirror to use a remote server instead of hosting games on my PC.
     
    Last edited: Apr 18, 2021
  16. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    I am using playfab too and I am pretty sure OnServerConnect works.
    I wrote a guide to setup playfab, maybe that will help you.
    It is not about mirror though, it is just about setting up playfab and unity just before you would add communication via mirror.
    https://jarc.xida.net/2021/04/06/playfab-multiplayer/
     
  17. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    9
    Thanks a ton. I'll take a look at your guide.
     
  18. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    9
    So I got it to work thanks to your guide. But here's where I'm stuck, again. I'm using Playfab matchmaking, I successfully matched two players and on match found I do this:

    Code (CSharp):
    1. private void OnGetMatch(GetMatchResult result)
    2.     {
    3.         UpdateStatusText($"{ result.Members[0].Entity.Id} vs { result.Members[1].Entity.Id}");
    4.        
    5.         //configure mirror network
    6.         NetworkManager.singleton.networkAddress = result.ServerDetails.IPV4Address;
    7.         NetworkManager.singleton.gameObject.GetComponent<KcpTransport>().Port = (ushort)result.ServerDetails.Ports[0].Num;
    8.  
    9.         NetworkManager.singleton.maxConnections = playerData.SelectedMap.MaxPlayers;
    10.         NetworkManager.singleton.onlineScene = playerData.SelectedMap.MapName;
    11.  
    12.         //start client
    13.         NetworkManager.singleton.StartClient();
    14.     }
    But nothing happens, the client just starts and shuts down. Should I make one of the players a host? and how would I go about doing this?
     
  19. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    Sorry, I didn't use playfab's matchmaking yet.
    But as a general tip:
    You can connect to the playfab server via remote desktop. Then you can check the logs of your unity server to find out what is going on.
     
    Kamzoki likes this.
  20. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I think someone made a playfab Mirror plugin already, check our discord.
    Wasn't it Microsoft even?
     
  21. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    9
    I only found the playmaker playfab package.

    Okay, here's another question that might help me. In mirror's NetworkManager.cs, there's a function called StartHost. If I called this function on the server build on play fab, then called StartClient on each client as they start the match, will this be equivalent to what we do locally via the network hud? (Start host, join client)
     
  22. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    Playfab works exactly as you would work locally.
    On Playfab there is a virtual windows or linux pc running with your server that you build from within unity.
    So yes everything on playfab works just as you would work locally.
    As I mentioned earlier you can even use remote desktop to connect to the current running server on playfab and check for any problems.

    If you use the network hud:
    You need to insert a ip and port into the hud.
    Which means yes, this will work the same for playfab.
    As soon as you got a running server on playfab and know the server's port and ip you can enter that into the hud.

    By the way: The best way to test locally is to open two unity instances at the same time.
    And to do this I highly recommend https://forum.unity.com/threads/edi...er-without-building-uecho-replacement.964388/
     
  23. Kamzoki

    Kamzoki

    Joined:
    Apr 25, 2016
    Posts:
    9
    Thanks a lot for the comprehensive response. I'll fiddle with it more now that I understand that playfab's environment is the same as my local pc.
     
  24. KarimGameil

    KarimGameil

    Joined:
    Jan 21, 2021
    Posts:
    1
    Hi guys, I have a weird problem.

    When I host the game on editor and join as a separate client on the same machine the host client moves normally but the other client moves VERY fast.

    and when I host from the build and login with client through the editor, the build host moves normally while the other one on the editor moves VERY slowly.. please help
     
  25. AlphaLulz

    AlphaLulz

    Joined:
    Sep 2, 2019
    Posts:
    51
    I have the camera for the player as a child of the player object. If the player object has a network identity component on it, the child camera stops rendering. The network identity is not set to server only. Is there a way to render the camera while it is still the child of an object with a network identity?
     
  26. boomerang4

    boomerang4

    Joined:
    Jan 4, 2018
    Posts:
    5
    Mirror, or mirror-users please help:

    Where is the proper/best place to call a loading screen from? Or, how do you pass info to clients during async scene load?

    I can get it working for the server just fine, but when it comes to communicating things like loading progress to clients it seems like there's a black hole that swallows hooks/syncvars and ClientRPCs during async scene load (no errors or anything, the functions calling the RPCs go through but the actual RPCs themselves just never happen). I'm trying to get around syncvars/RPC by using OnClient/ServerChangedScene and the like, but it feels very hacky and again the whole "progress" part just jumps from 0 to 100 for clients so kind of defeats the purpose. Been at this for hours! Help!
     
  27. jhbyhuangzejie

    jhbyhuangzejie

    Joined:
    Mar 10, 2017
    Posts:
    7
    I want to realize the hall function how to achieve, and what good plan?thank you
     
  28. Programmingat23505

    Programmingat23505

    Joined:
    Sep 15, 2020
    Posts:
    44
    I have integrated vivox communications with my game and the logon credentials are stored in a variable set in the inspector, if on start I set a sync var to the same set of strings will it update when I start the server? Or do I need to set them when the is actually started?
     
    Last edited: May 15, 2021
  29. jamesjenningsard

    jamesjenningsard

    Joined:
    Jun 26, 2020
    Posts:
    6
    Hi friends

    I have a fun little "remote control audio player" working pretty swell, thanks to Mirror. I have FMOD running the audio, triggered by a pretty basic [Command]/[ClientRPC] loop-around.

    On iOS, my FMOD continues to play after locking the screen, and the Mirror Client (from a distance, it seems like) is continuing to receive, and queue incoming ClientRPC's. Then processing them all, simultaneously, when the app resumes focus.

    That what it seems like is happening, anyway.

    Question: I'm curious if there's any trick to getting those commands to process when the screen is off. I know iOS is very picky about what gets processed in the background, so my hopes are modest, but I'm curious if there's any sort of method worth trying? Perhaps in conjunction with the mobile task invoker?

    I'm sure I'm just misunderstanding what's possible, but I figured I would check in to see if anyone had any insight.
     
  30. no00ob

    no00ob

    Joined:
    Dec 13, 2017
    Posts:
    65
    I know this seems like an odd problem but why is Mirror adding a NetworkIdentity component to one of my scene objects in a scene with no network managers or other mirror components present? It's very annoying when I'm trying to playtest my game and this one object keeps disappearing and I have to manually enable it.
     
  31. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    We can't see your screen from here, would need more details :)
     
  32. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    New Mirror GitHub is live.
    2021-05-21 - github page with dreamers_white.png
     
    VirtualPierogi, Ruchir and BenniKo like this.
  33. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    jesusluvsyooh and Ruchir like this.
  34. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Great news: we achieved a 25x reduction in Socket.ReceiveFrom/SendTo allocations with
    where-allocation

    Below is our Benchmark example, sending 1000 monsters to 1 connection via KCP.
    8.9 KB per flush before.
    364 B after.

    We might be able to reduce it to 0 B with Unity 2021.0.2a19, which is supposed to be released this week.

    No more native C DLL workarounds to avoid allocations :)
     
  35. BBernard

    BBernard

    Joined:
    Jun 3, 2021
    Posts:
    1
    Hi and WOW! Tell me, please, can we use this info in the 2nd part of our article? We'd like to dedicate it to unity and unity vulnerabilities. Thanks in advance, Brit
     
  36. FellowPlayer123

    FellowPlayer123

    Joined:
    Dec 23, 2016
    Posts:
    114
    Hi, so I'm trying to integrate Mirror with a new input system and camera and character controller.

    I don't know why, but this code causes the game to drop from around 180 FPS to around 50FPS!

    What is going on here?

    Code (CSharp):
    1. public override void OnStartAuthority()
    2. {
    3.     cameraRoot.SetActive(true);
    4.     MyGameActions.Player.Look.performed += ctx => OnLook(ctx.ReadValue<Vector2>());
    5.     MyGameActions.Player.Look.canceled += ctx => OnLook(ctx.ReadValue<Vector2>());
    6. }
    7.  
    8. [ClientCallback]
    9. private void OnEnable() => MyGameActions.Enable();
    10.  
    11. ClientCallback]
    12.  private void OnDisable() => MyGameActions.Disable();
    13.  
    14. // this causes game FPS to drop from ~180FPS to around ~50FPS!
    15. public void OnLook(Vector2 lookAxis)
    16. {
    17.     Debug.Log("testing");
    18. }
     
  37. FakeByte

    FakeByte

    Joined:
    Dec 8, 2015
    Posts:
    147
    Is OnLook called every frame? Calling Debu.Log is slow, try removing the log and see if that helps or if the problem is somewhere else.
     
    FellowPlayer123 likes this.
  38. FellowPlayer123

    FellowPlayer123

    Joined:
    Dec 23, 2016
    Posts:
    114
    Yeah, I forgot to write here, but removing Debug.Log was causing the trouble! I didn't know it's so expensive!


    Is there is some operation similar to if(Input.GetKeyDown("Space")),
    but in a new input system?

    Something like:

    Code (CSharp):
    1. void Update () {
    2.     if(MyGameActions.Player.Look.performed)
    3.     {
    4.     }
    5. }
     
  39. HeadZerg

    HeadZerg

    Joined:
    Aug 15, 2014
    Posts:
    19
    You could use
    Keyboard.current.space.wasPressedThisFrame
    .
     
  40. FellowPlayer123

    FellowPlayer123

    Joined:
    Dec 23, 2016
    Posts:
    114
    Yeah, but it's platform-specific. How about using an action map?
     
  41. HeadZerg

    HeadZerg

    Joined:
    Aug 15, 2014
    Posts:
    19
    In that case you could use
    InputAction.triggered
    and
    InputAction.ReadValue<>()
    . InputActions fields are drawn in the inspector by the way.
     
    FellowPlayer123 likes this.
  42. blablaalb

    blablaalb

    Joined:
    Oct 28, 2015
    Posts:
    53
    I need someones help. I'm trying to convert single-player game to multiplayer game played over lan.
    Now I have some objects referencing the player object: camera with camerafollow component for example. Those are local player specific game objects and don't need to be instantiated on server or on other players side.
    I"m struggling to dynamically obtain reference to the spawned player. I raise an event inside
    NetworkManager.OnClientConnect
    :
    Code (CSharp):
    1.     public event Action<NetworkIdentity> Connected;
    2.  
    3.     public override void OnClientConnect(NetworkConnection conn)
    4.     {
    5.         base.OnClientConnect(conn);
    6.  
    7.         Connected?.Invoke(conn.identity);
    8.     }
    and subscribe to the event:
    Code (CSharp):
    1.         internal void Start()
    2.         {
    3.             var nm = FindObjectOfType<NetworkManager>();
    4.             if (nm != null) nm.Connected += (x) =>
    5.             {
    6.                 _player = x.gameObject;
    7.             };
    8.         }
    but this throws NullReferenceException, the NetworkConnection.identity inside OnClientConnect in null
    System.NullReferenceException: Object reference not set to an instance of an object at Player.CameraFollow+<>c.<Start>b__7_0 (Mirror.NetworkIdentity x) [0x00001]

    I'm struggling to understand how can I dynamically obtain a reference to the spawned player. Why the NetworkConnection.identity is null inside OnClientConnect? I thought it should point to the spawned Player game object. How to get reference to the local player object after player connects to the server?
     
  43. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Mirror Stability progress Update.

    Core Test Coverage now at 79.6%
    -> 'Mirror', 'Mirror.Weaver' Namespaces
    -> Excluding Transports, Components, Editor UIs etc.
    -> Including all [Obsolete]s, so the actual coverage is even higher

    2021-06-17 - 79,6 percent - including obsoletes.png
     
    MilenaRocha, Zeohack and xVergilx like this.
  44. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    NetworkTransform V1 vs. V2 comparison:


    Snapshot Interpolation working beautifully.
    => ~90-100ms latency %15 packet drop.
    => Outside tested by a MOBA project that we can't name yet.

    V2 is currently open for review & testing:
    https://github.com/vis2k/Mirror/pull/2791
     
  45. FellowPlayer123

    FellowPlayer123

    Joined:
    Dec 23, 2016
    Posts:
    114
    Nice! Is this going to replace the NetworkTransform component? When can we expect this update to arrive in AssetStore?
     
  46. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    That's the plan.
    NT2 has heavy test coverage already and is ready to use.
    We just give people a couple of days to test in case we missed anything.
    So far only positive feedback.
     
    FellowPlayer123 likes this.
  47. BlastM

    BlastM

    Joined:
    Jun 17, 2021
    Posts:
    2
    Is there any way to know command successfully sent by client? I need to change a boolean inside function before sending command. But when the client has no internet for a very short time and reconnects (connection established before timeout) my boolean changed and my command didn't received by server. How can I solve this situation? I didn't want to put any delay to changing back my variable.
     
    Last edited: Jul 13, 2021
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Commands go over reliable channel.
    So they are either guaranteed delivered, or the connection disconnects.
     
  49. FariAnderson

    FariAnderson

    Joined:
    Jan 20, 2020
    Posts:
    37
    Hey mr.vis2k, Will mirror work after UNet removal from Unity? If yes then how? Cuz I've heard it's built on top of UNet.
     
  50. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes :)