- Home /
Hi. I have a question about making multi rate(frequency) input (parallel computing)
which I want to get to Unity is get position data from VIVE Controller(which is limited to 90 Hz only) for 90 Hz and get position data from mouse for 1000 Hz, with same time information(to say, trigger?)
is there any way to get different refresh rate inside unity or is it called parallel computing or multi-threading or is there any way to get mouse data by setting trigger of other program(~.exe) and receive data?
thank you for reading my question.
Answer by xxmariofer · Jun 27, 2019 at 06:45 PM
good and bad news, first of all, yes, you can use multithreading for "faster" refresh rates. BUT you cant call from a secondary thread to anything unityengine related. you can have a update loop like this using multithreading BUT you cant access the input mouse position (maybe you can try and using any oher library for finding mouse position) what you CAN do is use an Invoke method for example, that is called from Main thread so you can access unity input.position and is faster than the update BUT is not a second thread so is wont be as fast as a secondary thread (since in main thread is being done rendering physics...) and LAST issue is that Input.MousePosition is frame rate dependant so even if you manage to compare the UnityEngine.Input.mousePosition, this value isnt updated all the time, only at the start of each frame, so it will return always the same value, here is some code so you can test this
using System.Threading;
using UnityEngine;
public class Test : MonoBehaviour {
float countUpdate = 0;
float countInvoke = 0;
float countThread = 0;
Thread _updateThread;
// Use this for initialization
void Start () {
InvokeRepeating("InvokeMethod", 0f, 0.001f);
_updateThread = new Thread(SuperFastLoop);
_updateThread.Start();
}
// Update is called once per frame
void Update () {
countUpdate++;
Debug.Log("Update " + countUpdate);
}
public void InvokeMethod()
{
countInvoke++;
Debug.Log("INVOKE " + countInvoke);
}
private void SuperFastLoop()
{
// This begins our Update loop
while (true)
{
countThread++;
Debug.Log("THREAD " + countThread);
// This suspends the thread for 5 milliseconds, making this code execute 200 times per second
Thread.Sleep(5);
}
}
private void OnDestroy()
{
_updateThread.Abort();
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
some idea of creating consecutive missions in a scene. 1 Answer
Renderer on object disabled after level reload 1 Answer
Issue with my camera controls 0 Answers