在Unity中使用Arduino作为控制器给我不好的帧率

问题描述 投票:1回答:1

当您使用Arduino作为输入时,我的帧率下降到〜15fps,当我使用键盘作为输入时,帧率下降到〜25fps,但是如果我将Arduino完全排除在游戏之外,我的帧率将稳定地达到100 + fps。我的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);            
    }


}

如果有人可以识别此问题,将不胜感激

c# unity3d serial-port arduino-uno frame-rate
1个回答
0
投票

我想通了!所以这是我的固定代码

    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) {


                }
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.