Search Unity

Need Help :( MLAPI Player Movements | Networking

Discussion in 'Netcode for GameObjects' started by Pathfinder007_ZA, Aug 5, 2021.

  1. Pathfinder007_ZA

    Pathfinder007_ZA

    Joined:
    Jul 29, 2021
    Posts:
    15
    Hi all
    Hope you doing well.

    In the summer i started coding a 2D MMORPG in Java, however after countless hours of developing my own game engine i decided to move over to unity.

    I am still a beginner so i tackled some beginner networking projects. However i ran into a problem understanding how MLAPI works, specifically client player movement. I followed the Getting started with MLAPI which teaches you the basics.

    Could someone please help me understand how MLAPI networking works, my goal is to create a server sides game where by a client request changes from the server with the use of SERVER RPC. My first step id like to learn is client player movement, using WASD. Please help me understand this code so i can change the movement mechanics to WASD so i could move forward ;(
    Ive triend multiple different methods, i can get the server player to move perfectly however the networking part really messing me up.

    Code (CSharp):
    1. HelloWorldManager:
    2.  
    3. using MLAPI;
    4. using UnityEngine;
    5.  
    6. namespace HelloWorld
    7. {
    8. public class HelloWorldManager : MonoBehaviour
    9. {
    10. void OnGUI()
    11. {
    12. GUILayout.BeginArea(new Rect(10, 10, 300, 300));
    13. if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
    14. {
    15. StartButtons();
    16. }
    17. else
    18. {
    19. StatusLabels();
    20.  
    21. SubmitNewPosition();
    22. }
    23.  
    24. GUILayout.EndArea();
    25. }
    26.  
    27. // NetworkManager implements the singleton pattern as it declares its singleton named Singleton. This is defined when the //MonoBehaviour is enabled. This component also contains very useful properties, such as IsClient, IsServer, and IsLocalClient.
    28. // The first two dictate the connection state we have currently established that you will use shortly.
    29. // We call these methods inside of OnGUI().
    30.  
    31. static void StartButtons()
    32. {
    33. if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost();
    34. if (GUILayout.Button("Client")) NetworkManager.Singleton.StartClient();
    35. if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer();
    36. }
    37.  
    38. static void StatusLabels()
    39. {
    40. var mode = NetworkManager.Singleton.IsHost ?
    41. "Host" : NetworkManager.Singleton.IsServer ? "Server" : "Client";
    42.  
    43. GUILayout.Label("Transport: " +
    44. NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetType().Name);
    45. GUILayout.Label("Mode: " + mode);
    46. }
    47.  
    48. // SubmitNewPosition(). Defines player movement.
    49. // Whenever you press the GUI button (which is contextual depending on if you are server or a client), you find your local player and simply call Move().
    50.  
    51. static void SubmitNewPosition()
    52. {
    53. if (GUILayout.Button(NetworkManager.Singleton.IsServer ? "Move" : "Request Position Change"))
    54. {
    55. if (NetworkManager.Singleton.ConnectedClients.TryGetValue(NetworkManager.Singleton.LocalClientId,
    56. out var networkedClient))
    57. {
    58. var player = networkedClient.PlayerObject.GetComponent<HelloWorldPlayer>();
    59. if (player)
    60. {
    61. player.Move();
    62. }
    63. }
    64. }
    65. }
    66.  
    67. // One build instance can create a host. Another client can join the host's game. Both are able to press a GUI button to move. Server will move immediately and be replicated on client. Client can request a new position,
    68. // which will instruct the server to modify that server instance's position NetworkVariable. That client will apply that NetworkVariable position inside of it's Update() method.
    69.  
    70. }
    71. }
    72.  
    73. ---------------------------------------------------------------------------------------------------------------
    74.  
    75. HelloWorldPlayer
    76.  
    77. namespace HelloWorld
    78. {
    79. // This class will inherit from NetworkBehaviour instead of MonoBehaviour.
    80.  
    81. public class HelloWorldPlayer : NetworkBehaviour
    82. {
    83. // Inside this class we now define a NetworkVariable to represent this player's networked position.
    84. public NetworkVariableVector3 Position = new NetworkVariableVector3(new NetworkVariableSettings
    85. {
    86. WritePermission = NetworkVariablePermission.ServerOnly,
    87. ReadPermission = NetworkVariablePermission.Everyone
    88. });
    89.  
    90. // Introducing permissions#
    91. // In the HelloWorldPlayer.cs script we introduce read and write permissions on a NetworkVariable . For the purposes of this demo, the server will be authoritative on the NetworkVariable representing position. All clients are able to read the value,
    92. // however. HelloWorldPlayer overrides NetworkStart.
    93.  
    94. public override void NetworkStart()
    95. {
    96. Move();
    97. }
    98.  
    99. // Any MonoBehaviour implementing NetworkBehaviour can override the MLAPI method NetworkStart().
    100. // This method is fired when message handlers are ready to be registered and the networking is setup. We override NetworkStart since a client and a server will run different logic here.
    101.  
    102. // On both client and server instances of this player, we call the Move() method, which will simply do the following.
    103.  
    104.  
    105.  
    106. public void Move()
    107. {
    108.  
    109. // If this player is a server-owned player, at NetworkStart() we can immediately move this player, as suggested in the following code.
    110.  
    111. if (NetworkManager.Singleton.IsServer)
    112. {
    113. var randomPosition = GetRandomPositionOnPlane();
    114. transform.position = randomPosition;
    115. Position.Value = randomPosition;
    116. }
    117. else
    118. {
    119. // If we are a client, we call a ServerRpc. A ServerRpc can be invoked by a client to be executed on the server.
    120.  
    121. SubmitPositionRequestServerRpc();
    122. }
    123. }
    124.  
    125. [ServerRpc]
    126. // This ServerRpc simply sets the position NetworkVariable on the server's instance of this player
    127.  
    128. void SubmitPositionRequestServerRpc(ServerRpcParams rpcParams = default)
    129. {
    130. Position.Value = GetRandomPositionOnPlane();
    131. }
    132.  
    133. static Vector3 GetRandomPositionOnPlane()
    134. {
    135. return new Vector3(Random.Range(-3f, 3f), 1f, Random.Range(-3f, 3f));
    136. }
    137.  
    138. // The server instance of this player has just modified the Position NetworkVariable, meaning that if we are a client, we need to apply this position locally inside of our Update loop.
    139.  
    140. void Update()
    141. {
    142. transform.position = Position.Value;
    143. }
    144. }
    145. }
    146.  
    147. ---------------------------------------------------------------------------------------------------------------
     
    Last edited: Aug 5, 2021
  2. matta1989

    matta1989

    Joined:
    May 20, 2013
    Posts:
    27
    So you see where you call the submitpositionrequestserverrpc.. that method there is the one that makes the position moveable and when move() is called it takes a random position. You have to recode that segment to use wasd movement instead.