简体   繁体   中英

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

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

error but I can use the Arduino as inputs but it's just the keyboard that gets disabled for some reason

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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