简体   繁体   English

为 Unity 游戏精确模拟 xbox 控制器

[英]Precisely simulating an xbox controller for Unity game

I'm trying to simulate a controller via python but having issues with timing.我正在尝试通过 python 模拟控制器,但有时间问题。

I've successfully implemented reading the inputs into a dictionary , and then collecting all of them into a list.我已经成功地实现了将输入读入dictionary ,然后将它们全部收集到一个列表中。 I know that the game(in Unity) doesn't recognise inputs faster than 0.2s.我知道游戏(在 Unity 中)识别输入的速度不会超过 0.2 秒。 Using the following function, I've managed write at 0.2s with an error of less than 0.001s with the following function:使用以下函数,我使用以下函数在 0.2 秒内管理写入,错误小于 0.001 秒:

def feed(line: list, interval: float = 0.1.99):
    start = time_ns()
    wait_ns = interval * 10**9
    for moment in list:
        while (time_ns() < start + wait_ns):
            pass
        for key, val in moment:
            controller.set_value(key, val) #this feeds the keys to the game via pyxinput.

I'm sampling the input using a similar function as above.我正在使用与上述类似的函数对输入进行采样。 But yet, I'm not getting the correct behaviour, it seems to be a timing problem.但是,我没有得到正确的行为,这似乎是一个时间问题。 I tried reading the synthetic inputs, and they're being input with the correct timing, ie the interval that I pass in(and the aforementioned error).我尝试读取合成输入,并且它们以正确的时间输入,即我传入的间隔(以及上述错误)。

For reference, the following, hard coded input works correctly.作为参考,以下硬编码输入可以正常工作。

controller.set_value('AxisLy', -1) #push the stick down
sleep(0.2)
controller.set_value('AxisLy', 0) #return it to neutral

I'm at my wit's end after fighting with this for the past few days.在过去几天与此斗争后,我已经无所适从。 So, any ideas on what could be going wrong, or how I may debug it?那么,关于可能出什么问题的任何想法,或者我如何调试它?

Instead of hard coding in a sleep interval, try using Invoke() or a coroutine .尝试使用Invoke()协程,而不是在睡眠间隔中进行硬编码。 I find them very simple, useful and easy to understand when you're wanting to wait a certain period of time.当您想等待一段时间时,我发现它们非常简单、有用且易于理解。 Alternatively, use Time.time to track the start time of a button press, and then check if (Time.time >= startTime + interval) for system clock time.或者,使用 Time.time 跟踪按钮按下的开始时间,然后检查系统时钟时间是否为 (Time.time >= startTime + interval)。

Sorry for the C#, it's what i'm more familiar with.对不起 C#,这是我更熟悉的。 But you should be able to get the gist of it.但是你应该能够得到它的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM