C# 在Unity中使用Arduino作为控制器给了我糟糕的帧率
当你使用Arduino作为输入时,我的帧速率下降到约15fps,当我使用键盘作为输入时,帧速率下降到约25fps。然而,如果我将Arduino完全排除在游戏之外,我的帧速率将稳定在100+fps。我的游戏是2D的。这是我的密码C# 在Unity中使用Arduino作为控制器给了我糟糕的帧率,c#,unity3d,serial-port,arduino-uno,frame-rate,C#,Unity3d,Serial Port,Arduino Uno,Frame Rate,当你使用Arduino作为输入时,我的帧速率下降到约15fps,当我使用键盘作为输入时,帧速率下降到约25fps。然而,如果我将Arduino完全排除在游戏之外,我的帧速率将稳定在100+fps。我的游戏是2D的。这是我的密码 public class Player : MonoBehaviour { public float moveSpeed = 6; public float gravity = -20; public float jumpDistance = 8
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);
}
}
公共类玩家:单行为{
公共浮动速度=6;
公众浮标重力=-20;
公共浮标跳距=8;
向量3移动距离;
字节输入;
SerialPort流=新的SerialPort(“COM7”,9600);
控制器2D控制器;
void Start(){
控制器=GetComponent();
//sp.DtrEnable=true;
stream.ReadTimeout=100;
stream.Open();
}
无效更新(){
if(stream.IsOpen){
试一试{
arduinoInput=(字节)stream.ReadByte();
打印(输入);
}
捕获(系统异常){
}
}
如果(arduinoInput==2){//Als je de 2de drukknop indrukt
moveDistance.x=-moveSpeed;//Ga-je-links-bewegen
controller.Move(moveDistance*Time.deltaTime);//Leest de input
}
如果(arduinoInput==3){//Als je de 3de druknop indrukt
moveDistance.x=moveSpeed;//Ga je rechts bewegen
controller.Move(moveDistance*Time.deltaTime);//Leest de input
}
如果(controller.collisions.over | | controller.collisions.down){
移动距离y=0;
}
if((Input.GetKeyDown(KeyCode.Space)| | arduinoInput==1)和&controller.collisions.below){
moveDistance.y=跳线距离;//Je gaat springen langs de y-as
//移动距离x=0;
}
vector2input=newvector2(input.GetAxisRaw(“水平”)、input.GetAxisRaw(“垂直”);
moveDistance.x=输入.x*移动速度;
moveDistance.y+=重力*时间增量;
控制器移动(移动距离*时间增量);
}
}
如果有人能发现这个问题,我将不胜感激我已经解决了!这是我的固定代码
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) {
}
}
}
}
void Start(){
controller=GetComponent();//Je krijgt toegang tot de script Controller2D
stream.ReadTimeout=100;
stream.Open();//Uw serialpoort openen
Thread sampleThread=新线程(新线程开始(sampleFunction));
sampleThread.IsBackground=true;
sampleThread.Start();
}
无效更新(){
//如果(stream.IsOpen){//Als-uw-serialpoort打开
//试一试{
//arduinoInput=(字节)stream.ReadByte();
////打印(输入);
//Debug.Log(输入);
// }
//捕获(系统异常){
// }
//}
如果(arduinoInput==2){//Als je de 2de drukknop indrukt
moveDistance.x=-moveSpeed;//Ga-je-links-bewegen
controller.Move(moveDistance*Time.deltaTime);//Leest de input
}
如果(arduinoInput==3){//Als je de 3de druknop indrukt
moveDistance.x=moveSpeed;//Ga je rechts bewegen
controller.Move(moveDistance*Time.deltaTime);//Leest de input
}
如果(controller.collisions.before | | | controller.collisions.before){//Als je een Boting hebt van boven of beneden和ga je stoppen遇到了springen
移动距离y=0;
}
if((Input.GetKeyDown(KeyCode.Space)| | arduinoInput==1)和&controller.collisions.below){//Als je op Space drukt en Als je op een Als je op een platform staat dan ga je boven springen
moveDistance.y=跳线距离;//Je gaat springen langs de y-as
//moveDistance.x=0;//我是春天的春天,也是春天的春天
}
Vector2 input=new Vector2(input.GetAxisRaw(“水平”),input.GetAxisRaw(“垂直”);//Je neemt de Horizontal en Vertical inputs van de unity zelf
moveDistance.x=input.x*moveSpeed;//门输入与门连接
moveDistance.y+=重力*Time.deltaTime;//我和zwaartekracht相遇,我和sneller相遇。
controller.Move(moveDistance*Time.deltaTime);//Leest de input
}
public void sampleFunction(){
while(true){
如果(stream.IsOpen){//Als-uw-serialpoort打开
试一试{
arduinoInput=(字节)stream.ReadByte();
//打印(输入);
Debug.Log(输入);
}
捕获(系统异常){
}
}
}
}
stream.ReadByte()会等到有一个字节需要读取,这会降低游戏速度。尝试ReadAsync或在另一个线程中读取。您能给出一个如何在更新函数中读取线程的示例吗?我已经在start函数中启动了线程,并将stream.ReadByte()放入其中;在另一个线程中,但我不知道如何将其放在更新功能中我修复了它!非常感谢你引导我走上正确的道路,不客气。谢谢分享答案。