iboy15
iboy15

Reputation: 35

Unity control object via socket Udp C#

I have an object in unity and I want to control movement via udp using Visual Studio 2012 (C#). I've test send and receive chat and already successful.

And I've test script for move object ongui and its successful too. And now I want to control object transform or translate from command string that I got from udp receive but the object not moved.

Here is my code on receive

private  void ReceiveData()
{

    client = new UdpClient(port);
    while (true)
    {

        try
        {
            // Bytes empfangen.
            IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
            byte[] data = client.Receive(ref anyIP);

            // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
            string text = Encoding.UTF8.GetString(data);

        switch (text)
        {

        case "loc+x" :
        {
            satelit.transform.Translate (speedloc,0,0 * Time.deltaTime);
            break;
        }

        case "loc-x" :
        {
            satelit.transform.Translate (-speedloc,0,0 * Time.deltaTime);
            break;
        }

        }

            // Den abgerufenen Text anzeigen.
            print(">> " + text);

            // latest UDPpacket
            lastReceivedUDPPacket=text;                                                                                                                    

            // ....
            allReceivedUDPPackets=allReceivedUDPPackets+text;

        }
        catch (Exception err)
        {
            print(err.ToString());
        }
    }
}

The string that I send is received but the object can't move.

Upvotes: 0

Views: 944

Answers (1)

user11913565
user11913565

Reputation:

first, you have to check the value of "speedloc". If it's null or zero, your object won't move anyway.

also, I suggested that you should change this line from

satelit.transform.Translate (speedloc,0,0 * Time.deltaTime);

to

satelit.transform.Translate (new Vector3(speedloc,0,0) * Time.deltaTime);

Upvotes: 2

Related Questions