Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ivanshv · Jul 26, 2018 at 12:57 PM · networkingtransformtransform.positiondatavalues

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]) ;
                 }
         }
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Jul 28, 2018 at 05:38 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ivanshv · Jul 30, 2018 at 09:16 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges