C# 检测所有骰子何时停止移动
这似乎是一个很简单的问题,但我很难解决。我正在掷骰子(C# 检测所有骰子何时停止移动,c#,unity3d,coroutine,C#,Unity3d,Coroutine,这似乎是一个很简单的问题,但我很难解决。我正在掷骰子(GameObjects),试图检测它们何时停止移动(这样我就可以计算分数) 以下是我尝试过的: public class GameManager : MonoBehaviour { public GameObject[] _dice; public Vector3 _rollStartPosition; public float _rollForce; public float _rollTorque;
GameObject
s),试图检测它们何时停止移动(这样我就可以计算分数)
以下是我尝试过的:
public class GameManager : MonoBehaviour
{
public GameObject[] _dice;
public Vector3 _rollStartPosition;
public float _rollForce;
public float _rollTorque;
bool anyDieIsMoving = false;
void FixedUpdate()
{
if (!anyDieIsMoving && Input.GetMouseButtonDown(0))
RollDice();
}
void RollDice()
{
foreach (var die in _dice)
{
// Roll() adds force and torque from a given starting position
die.GetComponent<Die>()
.Roll(_rollStartPosition, Random.onUnitSphere * _rollForce, Random.onUnitSphere * _rollTorque);
}
StartCoroutine(CheckIfDiceAreMoving());
// Calculate score and do something with it...
}
IEnumerator CheckIfDiceAreMoving()
{
foreach (var die in _dice)
{
var dieRigidbody = die.GetComponent<Rigidbody>();
if (!dieRigidbody.IsSleeping())
{
anyDieIsMoving = true;
yield return null;
}
}
}
}
公共类游戏管理器:单一行为
{
公共游戏对象[]_骰子;
公共向量3_滚动开始位置;
公众浮力;
公共浮动(u);;
bool anyDieIsMoving=false;
void FixedUpdate()
{
如果(!anyDieIsMoving&&Input.GetMouseButtonDown(0))
滚动骰子();
}
void RollDice()
{
foreach(变量在骰子中)
{
//Roll()从给定的起始位置添加力和扭矩
die.GetComponent()
.Roll(_rollStartPosition,Random.onUnitSphere*_rollForce,Random.onUnitSphere*_rollTorque);
}
start例程(CheckIfDiceAreMoving());
//计算分数并用它做点什么。。。
}
IEnumerator CheckIfDiceAreMoving()
{
foreach(变量在骰子中)
{
var dieRigidbody=die.GetComponent();
如果(!dieRigidbody.IsSleeping())
{
anyDieIsMoving=true;
收益返回空;
}
}
}
}
上面代码的问题是,它会在所有骰子停止移动之前立即尝试计算分数(我通过添加一堆Debug.Log()
语句发现了这一点)
在计算分数之前,我如何等待所有骰子停止移动?您必须使
RollDice
成为一个协同程序函数,然后您可以放弃或等待CheckIfDiceAreMoving
函数返回yield return
。更好的方法是,将if(!diegidbody.IsSleeping())
转换为而(!diegidbody.IsSleeping())
,这样在所有骰子停止移动之前,CheckIfDiceAreMoving
函数不会退出。另外,检查更新
功能中的输入,而不是用于移动刚体的固定更新
这是一个带系数的代码:
public class GameManager : MonoBehaviour
{
public GameObject[] _dice;
public Vector3 _rollStartPosition;
public float _rollForce;
public float _rollTorque;
bool doneRolling = true;
void Update()
{
if (doneRolling && Input.GetMouseButtonDown(0))
{
StartCoroutine(RollDice());
}
}
IEnumerator RollDice()
{
doneRolling = false;
foreach (var die in _dice)
{
// Roll() adds force and torque from a given starting position
die.GetComponent<Die>()
.Roll(_rollStartPosition, Random.onUnitSphere * _rollForce, Random.onUnitSphere * _rollTorque);
}
//Wait until all dice tops moving
yield return CheckIfDiceAreMoving();
// Calculate score and do something with it...
//Set doneRolling to true so that we call this funtion again
doneRolling = true;
}
IEnumerator CheckIfDiceAreMoving()
{
foreach (var die in _dice)
{
var dieRigidbody = die.GetComponent<Rigidbody>();
//Wait until all dice stops moving
while (!dieRigidbody.IsSleeping())
{
yield return null;
}
}
}
}
公共类游戏管理器:单一行为
{
公共游戏对象[]_骰子;
公共向量3_滚动开始位置;
公众浮力;
公共浮动(u);;
bool doneRolling=true;
无效更新()
{
if(doneRolling&&Input.GetMouseButtonDown(0))
{
start例程(RollDice());
}
}
IEnumerator RollDice()
{
doneRolling=假;
foreach(变量在骰子中)
{
//Roll()从给定的起始位置添加力和扭矩
die.GetComponent()
.Roll(_rollStartPosition,Random.onUnitSphere*_rollForce,Random.onUnitSphere*_rollTorque);
}
//等待所有骰子顶部移动
收益返回checkIfdieCareMoving();
//计算分数并用它做点什么。。。
//将doneRolling设置为true,以便我们再次调用此函数
doneRolling=true;
}
IEnumerator CheckIfDiceAreMoving()
{
foreach(变量在骰子中)
{
var dieRigidbody=die.GetComponent();
//等待所有骰子停止移动
而(!dieRigidbody.IsSleeping())
{
收益返回空;
}
}
}
}
您必须使RollDice
成为一个协程函数,然后您可以放弃或等待CheckIfDiceAreMoving
函数与放弃返回一起返回。更好的方法是,将if(!diegidbody.IsSleeping())
转换为而(!diegidbody.IsSleeping())
,这样在所有骰子停止移动之前,CheckIfDiceAreMoving
函数不会退出。另外,检查更新
功能中的输入,而不是用于移动刚体的固定更新
这是一个带系数的代码:
public class GameManager : MonoBehaviour
{
public GameObject[] _dice;
public Vector3 _rollStartPosition;
public float _rollForce;
public float _rollTorque;
bool doneRolling = true;
void Update()
{
if (doneRolling && Input.GetMouseButtonDown(0))
{
StartCoroutine(RollDice());
}
}
IEnumerator RollDice()
{
doneRolling = false;
foreach (var die in _dice)
{
// Roll() adds force and torque from a given starting position
die.GetComponent<Die>()
.Roll(_rollStartPosition, Random.onUnitSphere * _rollForce, Random.onUnitSphere * _rollTorque);
}
//Wait until all dice tops moving
yield return CheckIfDiceAreMoving();
// Calculate score and do something with it...
//Set doneRolling to true so that we call this funtion again
doneRolling = true;
}
IEnumerator CheckIfDiceAreMoving()
{
foreach (var die in _dice)
{
var dieRigidbody = die.GetComponent<Rigidbody>();
//Wait until all dice stops moving
while (!dieRigidbody.IsSleeping())
{
yield return null;
}
}
}
}
公共类游戏管理器:单一行为
{
公共游戏对象[]_骰子;
公共向量3_滚动开始位置;
公众浮力;
公共浮动(u);;
bool doneRolling=true;
无效更新()
{
if(doneRolling&&Input.GetMouseButtonDown(0))
{
start例程(RollDice());
}
}
IEnumerator RollDice()
{
doneRolling=假;
foreach(变量在骰子中)
{
//Roll()从给定的起始位置添加力和扭矩
die.GetComponent()
.Roll(_rollStartPosition,Random.onUnitSphere*_rollForce,Random.onUnitSphere*_rollTorque);
}
//等待所有骰子顶部移动
收益返回checkIfdieCareMoving();
//计算分数并用它做点什么。。。
//将doneRolling设置为true,以便我们再次调用此函数
doneRolling=true;
}
IEnumerator CheckIfDiceAreMoving()
{
foreach(变量在骰子中)
{
var dieRigidbody=die.GetComponent();
//等待所有骰子停止移动
而(!dieRigidbody.IsSleeping())
{
收益返回空;
}
}
}
}
在RollDice()中,它在获得anydie移动状态之前计算分数。我想你应该把计算分数移动到协同程序
void RollDice()
{
foreach (var die in _dice)
{
// Roll() adds force and torque from a given starting position
die.GetComponent<Die>()
.Roll(_rollStartPosition, Random.onUnitSphere * _rollForce, Random.onUnitSphere * _rollTorque);
}
StartCoroutine(CheckIfDiceAreMoving());
}
IEnumerator CheckIfDiceAreMoving()
{
while(anyDieIsMoving){
anyDieIsMoving = false;
foreach (var die in _dice)
{
var dieRigidbody = die.GetComponent<Rigidbody>();
if (!dieRigidbody.IsSleeping())
{
anyDieIsMoving = true;
yield return null;
}
}
}
// Calculate score and do something with it...
}
void RollDice()
{
foreach(变量在骰子中)
{
//Roll()从给定的起始位置添加力和扭矩
die.GetComponent()
.Roll(_rollStartPosition,Random.onUnitSphere*_rollForce,Random.onUnitSphere*_rollTorque);
}
start例程(CheckIfDiceAreMoving());
}
IEnumerator CheckIfDiceAreMoving()
{
当(任何人都在移动){
anyDieIsMoving=false;
foreach(变量在骰子中)
{
var dieRigidbody=die.GetComponent();
如果(!d