我刚刚开始使用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;
}
}
这是因为你有一个无限循环。
bullet1shot = true;
while (bullet1shot)
{
player[1].transform.Translate(Vector2.left * Time.deltaTime * shotspeed);
}