Hi guys,
I am absolutely new to Unity and struggle with a very simple (as I expect) issue.
I’m currently working on the integration of Unity into our system of virtual reality for animals (fruit-flies, zebra-fishes, mice). Currently the setup consists of two main parts: tracking device (multiple cameras which track the position of an animal in 3d on every frame) and the engine (based on OpenSceneGraph), which receives the tracking data and adjusts the displayed environment according to the animal’s position and rotation.
My goal is to replace OpenSceneGraph with Unity. The biggest question for me now is what is the best way to send transform data of the animal to Unity (camera or actor)? Currently the communication runs via ROS, but we would like to get rid of it and run something based on “Server-Send events”.
I see that HTTP communication is quite flexible in Unity, however, all the examples and tutorials are either about reading data-bases and represent retrieved data as printed text, or about multiplayer networking.
In sum, what I need is the transform component to be updated every frame based on the data (x, y, z, pitch, roll, yaw values) retrieved from the server.
Any help, advices, guidelines are highly appreciated. Thanks a lot in advance.
EDIT
Here is the final working script (must be cleaned though). The object receives the coordinates from the server via UDP and updates Transform component properly.
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class PlayerBehavior : MonoBehaviour {
private UdpClient udpServer;
public GameObject cube;
private Vector3 tempPos;
private Thread t;
public float movementSpeed;
private long lastSend;
private IPEndPoint remoteEP;
private float[] transformPosition = new float[3] ;
void Start()
{
udpServer = new UdpClient(1234);
t = new Thread(() => {
while (true) {
this.receiveData();
}
});
t.Start();
t.IsBackground = true;
remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 41234);
}
void Update()
{
transform.position = new Vector3(transformPosition[0], transformPosition[1], transformPosition[2]) ;
}
private void OnApplicationQuit()
{
udpServer.Close();
t.Abort();
}
private void receiveData() {
byte[] data = udpServer.Receive(ref remoteEP);
if (data.Length > 0)
{
var str = System.Text.Encoding.Default.GetString(data);
Debug.Log("Received Data" + str);
string[] messageParts = str.Split(',');
transformPosition[0] = float.Parse(messageParts[0]) ;
transformPosition[1] = float.Parse(messageParts[1]) ;
transformPosition[2] = float.Parse(messageParts[2]) ;
}
}
}