Advertisement

Controlling a desktop game from a mobile phone (Android)

Started by November 16, 2017 10:55 AM
2 comments, last by NDraskovic 7 years ago
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:

void Start () {
        Test = "Nothing yet happened";
        NetworkServer.Listen(25000);
        NetworkServer.RegisterHandler(888, ServerReceiveMessage);
    }
   
    private void ServerReceiveMessage(NetworkMessage message)
    {
       
        StringMessage msg = new StringMessage();
        msg.value = message.ReadMessage<StringMessage>().value;

        if (!String.IsNullOrEmpty(msg.value))
        {
            Test = "Message received";
            string[] deltas = msg.value.Split('|');
            Horizontal = Convert.ToSingle(deltas[0]);
            Vertical = Convert.ToSingle(deltas[1]);

            TestScript.MoveForward(Vertical);

            TestScript.RotateAroundY(Horizontal);
        }
        else
        {
            Test = "Nothing received";
        }
    }

 

 
And client this:

 private void Connect()
    {
             client.Connect(IPAddress, 25000);      
    }

    void Start () {
        client = new NetworkClient();
        Connect();
      
    }
   
    void Update () {   

#if UNITY_ANDROID
        MobileTouches = Input.touches;

        if (MobileTouches.Length > 0)
        {
            for (int i = 0; i < MobileTouches.Length; i++)
            {
                if (MobileTouches[i].phase == TouchPhase.Moved)
                {
                    Horizontal = MobileTouches[i].deltaPosition.x;
                    Vertical = MobileTouches[i].deltaPosition.y;

                }else if(MobileTouches[i].phase == TouchPhase.Stationary)
                {
                    Connect();                 
                }
            }
        }
#elif UNITY_EDITOR      

        Horizontal = Input.GetAxis("Horizontal");
        Vertical = Input.GetAxis("Vertical");
#endif


        thumb.Translate(Vector3.up * Vertical * Time.deltaTime);
        thumb.Translate(Vector3.right * Horizontal * Time.deltaTime);

        SendControllerInfo();
    }

    static public void SendControllerInfo()
    {
        if (client.isConnected)
        {
            StringMessage msg = new StringMessage();
            msg.value = Horizontal + "|" + Vertical;
            client.Send(888, msg);
        }
    }

 

 
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

Check your firewall settings on the desktop, and make sure that there are no rules that could be blocking that port.

Also, install Wireshark and spend a few minutes working out how to filter the view of traffic. That should be able to show you whether data is arriving on the desktop machine at the port you have specified.

Also, saying that IPAddress is hard-coded isn't much help - what is it? Is it definitely the local network address of the desktop machine?

Advertisement
1 minute ago, Kylotan said:

Check your firewall settings on the desktop, and make sure that there are no rules that could be blocking that port.

Also, install Wireshark and spend a few minutes working out how to filter the view of traffic. That should be able to show you whether data is arriving on the desktop machine at the port you have specified.

Thanks, I'll try that. Now that I did some more research, I'm not 100% sure if the devices are actually on the same network, which might be the source of the problem. Thanks for the suggestion, It will be useful later on.

This topic is closed to new replies.

Advertisement