I receive the data which are X, Y, Z position and X, Y, Z rotation by Using UDP real time(60hz)
But I have some troubles with gameobject…
I think I complete string split and parse by using array.
Is there any problem in split and parse?
I have a problem about matching data(x,y,z pos and x,y,z rot) to Object(box).
I got this error message.
NullReferenceException: Object reference not set to an instance of an object
UDPReceive.Update () (at Assets/UDPReceive.cs:59)
I attached my code(which are c# and lua script)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
public class UDPReceive : MonoBehaviour
{
// receiving Thread
Thread receiveThread;
// udpclient object
UdpClient client;
// port
public int port = 8051; // define > init
// infos
public string lastReceivedUDPPacket = "";
float XPos;
float YPos;
float ZPos;
float XRot;
float YRot;
float ZRot;
private Transform box;
//game object
public GameObject tmp;
private static void Main()
{
UDPReceive receiveObj = new UDPReceive();
receiveObj.init();
string text = "";
do
{
text = Console.ReadLine();
}
while (!text.Equals("exit"));
}
public void Start()
{
init();
}
void Update()
{
parseDataToBox(lastReceivedUDPPacket);
tmp.transform.position = box.position;
tmp.transform.eulerAngles = box.eulerAngles;
}
void OnGUI()
{
Rect rectObj = new Rect(40, 10, 200, 400);
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.UpperLeft;
GUI.Box(rectObj, "\nLast Packet: \n" + lastReceivedUDPPacket, style);
}
private void init()
{
// Endpunkt definieren, von dem die Nachrichten gesendet werden.
print("UDPSend.init()");
// define port
port = 8051;
// status
print("Sending to 127.0.0.1 : " + port);
print("Test-Sending to this Port: nc -u 127.0.0.1 " + port + "");
receiveThread = new Thread(
new ThreadStart(ReceiveData));
receiveThread.IsBackground = true;
receiveThread.Start();
}
// Unity Application Quit Function
void OnApplicationQuit()
{
stopThread();
}
// Stop reading UDP messages
private void stopThread()
{
if (receiveThread.IsAlive)
{
receiveThread.Abort();
}
client.Close();
}
// receive thread
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);
// Data Received from simulation
print("Received Data >> " + text);
// latest UDPpacket
lastReceivedUDPPacket = text;
}
catch (Exception err)
{
print(err.ToString());
}
}
}
private void parseDataToBox ( string text )
{
try
{
string[] AgxData = text.Split(';');
//float[] floatData = Array.ConvertAll(text.Split(';'), float.Parse);
float[] PosData = new float[3];
float[] RotData = new float[3];
PosData[0] = float.Parse(AgxData[0]);
PosData[1] = float.Parse(AgxData[1]);
PosData[2] = float.Parse(AgxData[2]);
RotData[0] = float.Parse(AgxData[3]);
RotData[1] = float.Parse(AgxData[4]);
RotData[2] = float.Parse(AgxData[5]);
box.position = new Vector3(PosData[0], PosData[1], PosData[2]);
box.eulerAngles = new Vector3(RotData[0], RotData[1], RotData[2]);
}
catch (Exception err)
{
print(err.ToString());
}
}
}
This is Lua code.
socket = require("socket")
print(socket._VERSION)
function dataListener:post( t )
local XPos = ship.rb:getPosition():x()
local YPos = ship.rb:getPosition():y()
local ZPos = ship.rb:getPosition():z()
local XXPos = math.floor( XPos * 1000 + 0.5 ) / 1000
local YYPos = math.floor( YPos * 1000 + 0.5 ) / 1000
local ZZPos = math.floor( ZPos * 1000 + 0.5 ) / 1000
local XRot = ship.rb:getRotation():x()
local YRot = ship.rb:getRotation():y()
local ZRot = ship.rb:getRotation():z()
local XXRot = math.floor( XPos * 1000 + 0.5 ) / 1000
local YYRot = math.floor( YPos * 1000 + 0.5 ) / 1000
local ZZRot = math.floor( ZPos * 1000 + 0.5 ) / 1000
udp=socket.udp();
udp:setpeername("127.0.0.1",8051)
udp:send(string.format("%f; %f; %f; %f; %f; %f", XXPos, YYPos, ZZPos, XXRot, YYRot, ZZRot));
end