Использование Arduino в качестве контроллера в Unity дает мне плохую частоту кадров - PullRequest
1 голос
/ 02 февраля 2020

Моя частота кадров падает до ~ 15 кадров в секунду, когда вы используете Arduino в качестве ввода, ~ 25 кадров в секунду, когда я использую клавиатуру в качестве ввода, однако, если я полностью исключаю Arduino из моей игры, я получаю solid 100 + кадры в секунду. Моя игра в 2D. Вот мой код

public class Player : MonoBehaviour {

    public float moveSpeed = 6;
    public float gravity = -20;
    public float jumpDistance = 8;
    Vector3 moveDistance;
    byte arduinoInput;

    SerialPort stream = new SerialPort("COM7", 9600);   
    Controller2D controller;    
    void Start() {

        controller = GetComponent<Controller2D>();  
        //sp.DtrEnable = true;
        stream.ReadTimeout = 100;

        stream.Open();          
    }

    void Update() {


        if (stream.IsOpen) {    
            try {
                arduinoInput =  (byte) stream.ReadByte();
                print(arduinoInput);   
            }
            catch (System.Exception) {


            }
        }


        if (arduinoInput == 2) {       // Als je de 2de drukknop indrukt
            moveDistance.x = -moveSpeed;   // Ga je links bewegen
            controller.Move(moveDistance * Time.deltaTime);     // Leest de input
        }
        if (arduinoInput == 3) {       // Als je de 3de druknop indrukt
            moveDistance.x = moveSpeed;     // Ga je rechts bewegen
            controller.Move(moveDistance * Time.deltaTime);     // Leest de input
        }


        if (controller.collisions.above || controller.collisions.below ) {   
            moveDistance.y = 0;
        }
        if ((Input.GetKeyDown(KeyCode.Space) || arduinoInput == 1) && controller.collisions.below) {   
            moveDistance.y = jumpDistance;  // Je gaat springen langs de y-as
            //moveDistance.x = 0;     
        }

        Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); 
        moveDistance.x = input.x * moveSpeed;   
        moveDistance.y += gravity * Time.deltaTime;     
        controller.Move(moveDistance * Time.deltaTime);            
    }


}

Буду признателен, если кто-нибудь может определить эту проблему

1 Ответ

1 голос
/ 02 февраля 2020

Я понял это! Так вот мой фиксированный код

    void Start() {

        controller = GetComponent<Controller2D>();  // Je krijgt toegang tot de script Controller2D

        stream.ReadTimeout = 100;

        stream.Open();  // Uw serialpoort openen
        Thread sampleThread = new Thread(new ThreadStart(sampleFunction));
        sampleThread.IsBackground = true;
        sampleThread.Start();

    }

    void Update() {


        //if (stream.IsOpen) {    // Als uw serialpoort open is
        //    try {

        //        arduinoInput = (byte)stream.ReadByte();
        //        //print(arduinoInput);
        //        Debug.Log(arduinoInput);
        //    }
        //    catch (System.Exception) {


        //    }
        //}


        if (arduinoInput == 2) {       // Als je de 2de drukknop indrukt
            moveDistance.x = -moveSpeed;   // Ga je links bewegen
            controller.Move(moveDistance * Time.deltaTime);     // Leest de input
        }
        if (arduinoInput == 3) {       // Als je de 3de druknop indrukt
            moveDistance.x = moveSpeed;     // Ga je rechts bewegen
            controller.Move(moveDistance * Time.deltaTime);     // Leest de input
        }


        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

        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 


    }

    public void sampleFunction() {
        while (true) {

            if (stream.IsOpen) {    // Als uw serialpoort open is
                try {

                    arduinoInput = (byte)stream.ReadByte();
                    //print(arduinoInput);
                    Debug.Log(arduinoInput);
                }
                catch (System.Exception) {


                }
            }
        }
    }
...