我有以下代码,用于让用户飞行一段时间然后将其关闭。
void Booster() { StartCoroutine (Booster (airTime)); }
IEnumerator Booster(float airTime) { isJumping = true;
var e = sphere.localEulerAngles;
e.x = 0;
e.y = 0;
sphere.localEulerAngles = e;
float startPosY = -0.24f;
float finalPosY = -0.24f + 2.5f;
float xStartAngle = 0;
float xEndAngle = 90f;
float timer = 0f;
float timeJump = animTime*2.5f;
while (timer <= timeJump)
{
timer += Time.deltaTime;
float yPosTemp = 0;
float xAngleTemp = 0;
yPosTemp = Mathf.Lerp(startPosY,finalPosY,timer/timeJump);
xAngleTemp = Mathf.Lerp(xStartAngle,xEndAngle,timer/timeJump);
var s = sphere.localPosition;
s.y = yPosTemp;
sphere.localPosition = s;
var a = sphere.localEulerAngles;
a.x = xAngleTemp;
sphere.localEulerAngles = a;
//Debug.Log("Player Angles: " + sphere.localEulerAngles);
yield return null;
}
timer = 0f;
while (timer <= airTime)
{
timer += Time.deltaTime;
yield return null;
}
timer = 0f;
var hitColliders = Physics.OverlapSphere(sphere.position, 10);
int i = 0;
//Time.timeScale = 1.25f;
while (hitColliders [i].name == "Track_tiny")
{
i += 1;
}
print(hitColliders[i].name);
hitTile = hitColliders[i].gameObject;
Debug.Log("Position: " + hitTile.transform.parent.gameObject.transform.localPosition + "Angle: " + hitTile.transform.parent.gameObject.transform.localEulerAngles);
for(int j = 0; j < gameManager.GameCubes.Count;j++)
{
if(hitColliders[i].name == gameManager.GameCubes[j].name)
{
spawnChecker = j + 1;
break;
}
}
float zStartAngle = hitTile.transform.parent.gameObject.transform.localEulerAngles.z;
float zStartPos = hitTile.transform.parent.gameObject.transform.localPosition.z;
float zEndPos = transformParent.localPosition.z;
float zEndAngle = sphere.localEulerAngles.z;
if((zStartAngle - zEndAngle) > 0)
{
if((zStartAngle - zEndAngle) > 180)
{
zStartAngle = -(360 - zStartAngle);
}
else if((zStartAngle - zEndAngle) <= 180)
{
zStartAngle = zStartAngle;
}
}
else if((zStartAngle - zEndAngle) < 0)
{
if((zEndAngle - zStartAngle > 180))
{
zEndAngle = -(360 - zEndAngle);
}
else if((zEndAngle - zStartAngle) <= 180)
{
zEndAngle = zEndAngle;
}
}
欢迎任何建议。谢谢。
答案 0 :(得分:0)
我不建议将其命名为相同的功能。我之所以这样说是因为Unity能够通过 StopCoroutine(Booster())来阻止你的协程(我觉得很奇怪)。但是,我不知道Unity在幕后做了什么。另外,在这一行:
while (hitColliders [i].name == "Track_tiny")
检查 [i] 是否高于或等于 hitColliders.Length 。