Control Object From UDP Socket

i have an object in unity and i want to control movement via udp using visual studio 2012 (c#) i’ve test send and recieve chat and already sucess fulll and i’ve test script for move object ongui and its succes full to… and now i wan 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 stil didn’t move

1 Answer

1

First of all, check if you’re entering the case labels. It’s a common mistake that there might be some additional spaces / line feeds at the beginning or at the end of your string.

Another thing that doesn’t make much sense are your two translate lines:

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

You only multiply the last element by Time.deltaTime which is “0”. Since this line is only executed once when you receive a package, it makes no sense to use Time.deltaTime at all.

Finally is your “ReceiveData” method running on a seperate thread? if so you can’t use Translate since all methods / properties from the Unity API are not thread safe.

A little bit more context would help i guess.

thanks man the problem is my "RecieveData" method running on seperate thread but, can you give me a clue what i must do to move object from udp socket ?

Well, you need to synchronise your data between the two threads. One of the easiest approaches for Unity is to use the [Loom class][1] which WhyDoIDoIt has written. It allows you to schedule a piece of code on the Unity main thread from any other thread. What you actually synchronise is up to you. You could just pass the string received and parse the string on the main thread, or parse it in your seperate thread and synchronise each action. [1]: http://unitygems.com/threads/