Search Unity

Controlling a desktop game with a mobile phone (Android)

Discussion in 'Multiplayer' started by NDraskovic_LGL, Nov 16, 2017.

  1. NDraskovic_LGL

    NDraskovic_LGL

    Joined:
    Oct 5, 2017
    Posts:
    10
    Hey guys,

    As the title says, I'm trying to control a desktop game by using my mobile phone as a controller.
    I created two scenes, one that acts as a server, other as a client.

    Server has this code:
    Code (CSharp):
    1. void Start () {
    2.         Test = "Nothing yet happened";
    3.         NetworkServer.Listen(25000);
    4.         NetworkServer.RegisterHandler(888, ServerReceiveMessage);
    5.     }
    6.    
    7.     private void ServerReceiveMessage(NetworkMessage message)
    8.     {
    9.        
    10.         StringMessage msg = new StringMessage();
    11.         msg.value = message.ReadMessage<StringMessage>().value;
    12.  
    13.         if (!String.IsNullOrEmpty(msg.value))
    14.         {
    15.             Test = "Message received";
    16.             string[] deltas = msg.value.Split('|');
    17.             Horizontal = Convert.ToSingle(deltas[0]);
    18.             Vertical = Convert.ToSingle(deltas[1]);
    19.  
    20.             TestScript.MoveForward(Vertical);
    21.  
    22.             TestScript.RotateAroundY(Horizontal);
    23.         }
    24.         else
    25.         {
    26.             Test = "Nothing received";
    27.         }
    28.     }
    And client this:
    Code (CSharp):
    1.   private void Connect()
    2.     {
    3.              client.Connect(IPAddress, 25000);      
    4.     }
    5.  
    6.     void Start () {
    7.         client = new NetworkClient();
    8.         Connect();
    9.      
    10.     }
    11.    
    12.     void Update () {  
    13.  
    14. #if UNITY_ANDROID
    15.         MobileTouches = Input.touches;
    16.  
    17.         if (MobileTouches.Length > 0)
    18.         {
    19.             for (int i = 0; i < MobileTouches.Length; i++)
    20.             {
    21.                 if (MobileTouches[i].phase == TouchPhase.Moved)
    22.                 {
    23.                     Horizontal = MobileTouches[i].deltaPosition.x;
    24.                     Vertical = MobileTouches[i].deltaPosition.y;
    25.  
    26.                 }else if(MobileTouches[i].phase == TouchPhase.Stationary)
    27.                 {
    28.                     Connect();                
    29.                 }
    30.             }
    31.         }
    32. #elif UNITY_EDITOR      
    33.  
    34.         Horizontal = Input.GetAxis("Horizontal");
    35.         Vertical = Input.GetAxis("Vertical");
    36. #endif
    37.  
    38.  
    39.         thumb.Translate(Vector3.up * Vertical * Time.deltaTime);
    40.         thumb.Translate(Vector3.right * Horizontal * Time.deltaTime);
    41.  
    42.         SendControllerInfo();
    43.     }
    44.  
    45.     static public void SendControllerInfo()
    46.     {
    47.         if (client.isConnected)
    48.         {
    49.             StringMessage msg = new StringMessage();
    50.             msg.value = Horizontal + "|" + Vertical;
    51.             client.Send(888, msg);
    52.         }
    53.     }
    Ip address is hard coded, I just replaced it with the "IpAddress" variable. The code itself builds fine, and when I try to run in on a desktop computer, it works as expected (just a simple movement of an object on the server screen). However when I try to publish the client scene to a mobile device (Android), it doesn't connect to the server. They are both connected to the same network.
    Can anyone tell me what the problem might be?

    Thanks
     
  2. Moe_Baker

    Moe_Baker

    Joined:
    Oct 22, 2017
    Posts:
    36
    NDraskovic_LGL likes this.
  3. NDraskovic_LGL

    NDraskovic_LGL

    Joined:
    Oct 5, 2017
    Posts:
    10
  4. imferozi

    imferozi

    Joined:
    Sep 4, 2019
    Posts:
    1
    how to input a button from client and receive it to server.?