- Home /
Update transform component from server every frame
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]) ;
}
}
}
from what I've learned about networking, which I'm not an expert in, you'll have to use sockets you support data pushes from server to client. once done it's just a matter of matching that data with the corresponding transform.
Answer by Bunny83 · Jul 28, 2018 at 01:25 PM
You don't want to use HTTP for things like that. First of all HTTP is not meant for continuous data transfer. It's a request - response protocol and has quite a bit of overhead per request since it's text based. While it's possible to use HTTP for continuous data transfer with methods like long polling, most of the HTTP stuff that Unity ships is only for typical request- response communication.
The best solution would probably be a raw TCP connection or using UDP packets. TCP might introduce some lag if the connection is bad. However when you only connect locally this doesn't matter. Note that client / server only really exists in a connection based protocol like TCP. UDP is connectionless. To receive any packets a peer has to actively receive packets on a given port.
Finally keep in mind that the visual update rate and the send rate of your data most likely won't be in sync. Receiving network traffic is usually handled asynchronously in a thread. For realtime simulation / visualization you usually just use the latest information that arrived. If the sendrate is lower than the visual update you may need to extrapolate. Most games actually have a lower send rate than update rate. They usually artificially let the simulation lag behind for some milli seconds to be able to smoothly interpolate towards the latest received data.
After all we don't know anything about your specifications (number of objects, update rate, ...) and this question is not really Unity related. Network communication with external applications through sockets only involve pure .NET / Mono classes. UnityAnswers is for specific questions about Unity that can be answered.
Thank you very much for such a detailed answer, @Bunny83. TCP and UDP connections are the first choice for us to test right now. However, we also keep the Websockets option in $$anonymous$$d.
Also, in the future I will pay more attention to what can be posted in UnityAnswers and what can not. Thank you for your response anyways.
Your answer
Follow this Question
Related Questions
How do I update values and the transform of an object on the server's and client's side? 0 Answers
Unable to apply "transform" on gameobject from Raycast 1 Answer
Attaching Object to mouse 1 Answer
How can i fix a position of a gameobject after it meets its condition 1 Answer
Alternative to using transform.translate and transform.position for moving objects exact values? 1 Answer