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
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 ?
– iboy15Well, 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/
– Bunny83