简体   繁体   English

如何同时使用 arduino 控制器和键盘?

[英]How do i use an arduino controller and a keyboard at the same time?

How do I use a controller and keyboard at the same time?如何同时使用控制器和键盘?

So I use an Arduino as my controller using ReadByte() as my input Here is my script for my player所以我使用 Arduino 作为我的控制器,使用ReadByte()作为我的输入 这是我的播放器脚本

void Start() 
{      
    controller = GetComponent<Controller2D>();  // Je krijgt toegang tot de script Controller2D
    sp.DtrEnable = true;
    sp.ReadTimeout = 100;       

    sp.Open();  // Uw serialpoort openen      
}

void Update() 
{
    if (sp.IsOpen)    // Als uw serialpoort open is
    {
        try
        {
            print(sp.ReadByte());   // Ga je de inkomende waarde lezen
        }
        catch (System.Exception) { }
    }      

    if (controller.collisions.above || controller.collisions.below)    // Als je een botsing hebt van boven of beneden dan ga je stoppen met springen
    {
        moveDistance.y = 0;
    }

    if (Input.GetKeyDown(KeyCode.Space) || sp.ReadByte() == 1 && controller.collisions.below)   // Als je op spatie drukt en als je op een platform staat dan ga je boven springen
    {
        moveDistance.y = jumpDistance;  // Je gaat springen langs de y-as
        //moveDistance.x = 0;     // Als je alleen springt dan ga je loodrecht boven en niet schuin
    }

    Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));  // Je neemt de Horizontal en vertical inputs van de unity zelf

    if (sp.ReadByte() == 2)       // Als je de 2de drukknop indrukt
    {
        moveDistance.x = -moveSpeed ;   // Ga je links bewegen
    }
    if (sp.ReadByte() == 3)        // Als je de 3de druknop indrukt
    {
        moveDistance.x = moveSpeed;     // Ga je rechts bewegen
    }

    moveDistance.x = input.x * moveSpeed;   // Door input kan je nu links of rechts bewegen met de pijlen
    moveDistance.y += gravity * Time.deltaTime;     // Je valt met een zwaartekracht dus je gaat sneller en sneller vallen.       
    controller.Move(moveDistance * Time.deltaTime);     // Leest de input 
}

Normally I would want to have both controller and keyboard as my inputs but after I run this game I immediatly get a通常我希望同时使用控制器和键盘作为我的输入,但是在我运行这个游戏后,我立即得到一个

TimeoutException: the operation has timed out TimeoutException:操作已超时

error but I can use the Arduino as inputs but it's just the keyboard that gets disabled for some reason错误,但我可以使用 Arduino 作为输入,但它只是由于某种原因被禁用的键盘

Might be the cause of the error not sure but in general I would not use sp.ReadByte() repeatedly but only once , store the value and compare that:可能是错误的原因不确定,但一般来说我不会重复使用sp.ReadByte()而只使用一次,存储值并比较:

void Update() 
{
    byte arduinoInput = 0;
    if (sp.IsOpen)    // Als uw serialpoort open is
    {
        try
        {
            arduinoInput  = sp.ReadByte();
            print(arduinoInput);   // Ga je de inkomende waarde lezen
        }
        catch (System.Exception) { }
    }      

    if (controller.collisions.above || controller.collisions.below)    // Als je een botsing hebt van boven of beneden dan ga je stoppen met springen
    {
        moveDistance.y = 0;
    }

    if (Input.GetKeyDown(KeyCode.Space) || arduinoInput == 1 && controller.collisions.below)   // Als je op spatie drukt en als je op een platform staat dan ga je boven springen
    {
        moveDistance.y = jumpDistance;  // Je gaat springen langs de y-as
        //moveDistance.x = 0;     // Als je alleen springt dan ga je loodrecht boven en niet schuin
    }

    Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));  // Je neemt de Horizontal en vertical inputs van de unity zelf

    if (arduinoInput == 2)       // Als je de 2de drukknop indrukt
    {
        moveDistance.x = -moveSpeed ;   // Ga je links bewegen
    }
    if (arduinoInput == 3)        // Als je de 3de druknop indrukt
    {
        moveDistance.x = moveSpeed;     // Ga je rechts bewegen
    }

    moveDistance.x = input.x * moveSpeed;   // Door input kan je nu links of rechts bewegen met de pijlen
    moveDistance.y += gravity * Time.deltaTime;     // Je valt met een zwaartekracht dus je gaat sneller en sneller vallen.       
    controller.Move(moveDistance * Time.deltaTime);     // Leest de input 
}

The errors are probably thrown for frames when the port is not open yet bt you already tried to read or simply caused by the multiple access.当端口尚未打开但您已经尝试读取或仅由多次访问引起时,可能会为帧抛出错误。

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

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