尝试重复检查变量时出现 StackOverflow 错误
StackOverflow error when trying to repeatedly check a variable
嘿,在我开始之前,我想让大家知道我不仅是这个论坛的新手,而且是 Unity 和 C# 本身的新手,所以如果有简单的解决方案或其他愚蠢的错误,我深表歉意。
好吧,基本上我想做的是在玩家击中 space 时切换玩家的重力,为了实现这一点,我正在检查玩家 transform.position.y 并查看它是否在它的位置指定高度,如果不是我加力。
编码区:
private void ChangeGravity()
{
if (rb.position.y >= 10f)
{
SAF = false;
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
}
else
{
rb.AddForce(0, VerticalForce * Time.deltaTime, 0);
ChangeGravity();
}
}
需要说明的是,SAF 是一种预防措施,因此玩家无法向 space 按钮发送垃圾邮件。
另外 VerticalForce = 2f 并且通过我的测试我确定 if 语句可能为真(此测试是通过将 y 设置为 10)
现在是错误:
WhosebugException
UnityEngine.Rigidbody.AddForce (Vector3 force, ForceMode mode)
UnityEngine.Rigidbody.AddForce (Single x, Single y, Single z) (at C:/buildslave/unity/build/Runtime/Dynamics/ScriptBindings/Dynamics.bindings.cs:171)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:21)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:22)
(The final line repeats a bunch but I cut that out)
整个脚本:The Script
编辑
我终于找到了一个非常有用的教程,如果没有你们我就找不到关于如何反转物体重力的教程,这使得这个问题过时了,谢谢你的时间我很抱歉我没有找到这个在提出问题之前。
您的方法不会 return,它会调用自身,而后者又会再次调用自身。
由于调用函数会在线程的堆栈上分配一些内存,因此堆栈很快就会溢出,因为它有几兆字节的 space 限制。
在while循环中调用该方法,满足条件时跳出循环。因此,在循环的每次迭代中,方法都会被 returned,调用堆栈不会增长。
while (true)
{
if (rb.position.y >= 10f)
{
SAF = false;
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
break; //break the loop since condition is met
}
else
{
rb.AddForce(0, VerticalForce * Time.deltaTime, 0);
continue; //the condition is not met, so the loop goes on
}
}
嘿,在我开始之前,我想让大家知道我不仅是这个论坛的新手,而且是 Unity 和 C# 本身的新手,所以如果有简单的解决方案或其他愚蠢的错误,我深表歉意。
好吧,基本上我想做的是在玩家击中 space 时切换玩家的重力,为了实现这一点,我正在检查玩家 transform.position.y 并查看它是否在它的位置指定高度,如果不是我加力。
编码区:
private void ChangeGravity()
{
if (rb.position.y >= 10f)
{
SAF = false;
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
}
else
{
rb.AddForce(0, VerticalForce * Time.deltaTime, 0);
ChangeGravity();
}
}
需要说明的是,SAF 是一种预防措施,因此玩家无法向 space 按钮发送垃圾邮件。 另外 VerticalForce = 2f 并且通过我的测试我确定 if 语句可能为真(此测试是通过将 y 设置为 10)
现在是错误:
WhosebugException
UnityEngine.Rigidbody.AddForce (Vector3 force, ForceMode mode)
UnityEngine.Rigidbody.AddForce (Single x, Single y, Single z) (at C:/buildslave/unity/build/Runtime/Dynamics/ScriptBindings/Dynamics.bindings.cs:171)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:21)
PlayerMovement.ChangeGravity () (at Assets/Scripts/PlayerMovement.cs:22)
(The final line repeats a bunch but I cut that out)
整个脚本:The Script
编辑
我终于找到了一个非常有用的教程,如果没有你们我就找不到关于如何反转物体重力的教程,这使得这个问题过时了,谢谢你的时间我很抱歉我没有找到这个在提出问题之前。
您的方法不会 return,它会调用自身,而后者又会再次调用自身。
由于调用函数会在线程的堆栈上分配一些内存,因此堆栈很快就会溢出,因为它有几兆字节的 space 限制。
在while循环中调用该方法,满足条件时跳出循环。因此,在循环的每次迭代中,方法都会被 returned,调用堆栈不会增长。
while (true)
{
if (rb.position.y >= 10f)
{
SAF = false;
rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
break; //break the loop since condition is met
}
else
{
rb.AddForce(0, VerticalForce * Time.deltaTime, 0);
continue; //the condition is not met, so the loop goes on
}
}