c# - 为什么当我水平移动精灵时 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;
}
}
解决方案
这是因为你有一个无限循环。
bullet1shot = true;
while (bullet1shot)
{
player[1].transform.Translate(Vector2.left * Time.deltaTime * shotspeed);
}
推荐阅读
- powershell - 提取和排序大文本文件
- html - 如何隐藏元素?
- c - 检查指针是否为 64 字节对齐时出错
- python - 绘制标准偏差与带宽平均值的比率
- c++ - 如何在不安装 Visual Studio 的情况下在 cmake 中使用 Visual Studio 生成器
- django - Django 上的数据表、服务器端和分页
- python - 使用 openpyxl 从 Excel 获取列宽
- c++ - 如何将声明类型作为参数发送
- r - 为什么无法将值分配给数据框中的空白位置,返回“替换有 1 行,数据有 0 行”
- c# - 在 Microsoft.Office.Interop.Word.Lists 集合上使用 LINQ