Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How to receive UDP packets from Opentrack in Unity?

Discussion in 'Multiplayer' started by Looki2000, Jul 28, 2022.

  1. Looki2000


    Mar 18, 2022
    Opentrack is software for head tracking for example using webcam that can send xyz position and rotation (although i'm only interested in position) through UDP. How can I reveive these UDP packets with Unity as simple as possible without any external libraries?
    I would also want to avoid freezing main update loop with that but i have no idea on how to do that. I'm pretty new to Unity and have no idea how to do something like that. I couldn't find any code on the internet that wouldn't freeze the update loop.
  2. etiennef


    Apr 1, 2015
    Hello Looki2000,

    a simple way to send and receive udp data would be to use UdpClient class which is pretty straightforward to use:

    You are facing the main loop freeze problem, because Unity is running on a single thread,
    and UdpClient.Receive is a blocking call (loops inifinitely until it receives data).

    So you have multiple options here:
    -> Use multiple threads: You can move this blocking call on another thread and set a signal when it's
    receiving data and process it in the update loop

    -> You can use Socket instead of UdpClient and use the Poll method to check if there is any data available,
    for example: if(m_Socket.Available > 0 && m_Socket.Poll(0, SelectMode.SelectRead))
    And if any, process that data.

    I believe that you already know how to handle byte[] you'll receive, if not, checkout BitConverter class (

  3. mgear


    Aug 3, 2010
    can also find examples with: unity udp async (this doesn't block main thread either).
    etiennef likes this.