每次运行此脚本时Unity都会崩溃

时间:2016-03-30 13:58:16

标签: c# android unity3d

我认为它与while有关,因为如果我删除它就可以了。

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
public float speed = 1f;
public float pos = 0f;
void Start () {

}

void Update () {

    float temp = Input.acceleration.x;
    while (pos >= -200f && pos <= 200f){

        if (Input.touchCount > 0) {
            if ((Input.GetTouch (0).position.x >= 250)) { //Right
                transform.Rotate (0, 0, 0);
                pos += speed * 500 * Time.deltaTime;
                transform.Translate (speed * 500 * Time.deltaTime, 0, 0,   Space.World);
            } else if ((Input.GetTouch (0).position.x < 250)) { //Left
                transform.Rotate (0, 0, 0);
                pos -= speed * 500 * Time.deltaTime;
                transform.Translate (-speed * 500 * Time.deltaTime, 0, 0, Space.World);
                //Time.timeScale = 0.2F;

            }
        }
    }
    //Time.timeScale = 1F;
    transform.Rotate (0, 0, -temp * 500 * Time.deltaTime); //Rotation

    }
}

它基本上是一个使用android加速度计和触摸屏控制立方体的脚本。它运行正常,但是当我实现“绑定”运动时,它会在我播放时崩溃。

2 个答案:

答案 0 :(得分:4)

你不应该在while中使用Update循环,因为Update是你的游戏循环。这实际上阻止了游戏的更新,因为它被困在你的循环中并且永远无法渲染下一帧。尝试使用if声明代替while来产生您想要的效果。

答案 1 :(得分:0)

只是改写lase:

只要您的游戏正在运行,它就会运行,并且只能在活动的GameObjects上运行。 因此,您不应在更新功能中使用While循环。

使用:

if (pos >= -200f && pos <= 200f){

}
相关问题