首页 > 解决方案 > 为什么当我水平移动精灵时 Unity 会崩溃?

问题描述

我刚启动 Unity,我正在制作一个玩家(精灵)的 2D 游戏,当我单击特定按钮时会射出箭头。玩家垂直移动,箭头水平移动。播放器移动正常,但是当我点击射箭时,Unity 崩溃,我必须从任务管理器中关闭它。

代码显示 0 个错误,除 Unity 外,其他一切正常。

我不知道问题出在哪里,我将显示整个代码;发射箭头的函数被调用shootingsysytem

void Start()
{
    health = 100;
    playerspeed = 2;
    shotspeed = 3;
    bulletcount = 3;
    playerpos();
    if (delay <= 0)
    {
        delay = 0;
    }
    stopbullets();
}

// Update is called once per frame
void Update()
{
    playermouvement();
    shootingsystem();
    playerpos();
    delay -= Time.deltaTime;
}

public void playermouvement()// works
{
    if (Input.GetKey("z"))
    {
        player[0].transform.Translate(Vector2.up * Time.deltaTime * playerspeed);
    }

    if (Input.GetKey("s"))
    {
        player[0].transform.Translate(Vector2.down * Time.deltaTime * playerspeed);
    }
}// works

public void shootingsystem() // this function shoots the arrows.........
{
    if (Input.GetKey("k"))
    {
        if (bulletcount == 3 & delay <= 0.0f)
        {
            bullet3shot = true;
            while (bullet3shot)
            {
                player[3].transform.Translate(Vector2.left * Time.deltaTime * shotspeed);
            }
            if (delay <= 0.0f)
            {
                bulletcount--;
                delay = 1;
            }
        }
        else if (bulletcount == 2 & delay <= 0.0f)
        {
            bullet2shot = true;
            while (bullet2shot)
            {
                player[2].transform.position += goingside * Time.deltaTime;
            }
            if (delay <= 0.0f)
            {
                bulletcount--;
                delay = 1;
            }
        }
        else if (bulletcount == 1 & delay <= 0.0f)
        {
            bullet1shot = true;
            while (bullet1shot)
            {
                player[1].transform.Translate(Vector2.left * Time.deltaTime * shotspeed);
            }
            if (delay <= 0.0f)
            {
                bulletcount--;
                bulletcount = 3; // remove later 
                delay = 1;
            }
        }
    }
}

public void playerpos()//works
{
    playerposition = player[0].transform.position;
    if (!bullet1shot)
    {
        player[1].transform.position = playerposition;
    }
    if (!bullet2shot)
    {
        player[2].transform.position = playerposition;
    }
    if (!bullet3shot)
    {
        player[3].transform.position = playerposition;
    }
}//works

private void stopbullets()
{
    if (player[1].transform.position.x <= -13) //stop first bullet
    {
        bullet1shot = false;
    }
    if (player[2].transform.position.x <= -13) //stop second bullet
    {
        bullet2shot = false;
    }
    if (player[3].transform.position.x <= -13) //stop third bullet
    {
        bullet3shot = false;
    }
}

标签: c#visual-studiounity3d

解决方案


这是因为你有一个无限循环。

            bullet1shot = true;
            while (bullet1shot)
            {
                player[1].transform.Translate(Vector2.left * Time.deltaTime * shotspeed);
            }

推荐阅读