我已经解决了计时器并记录了冲过终点线的人的时间。但我不知道如何计算谁是第一个冲过终点线的人。有人可以给我一些想法这是我的代码
public float timer = 0f;
float currentTimeBot1 = 0f;
float currentTimeBot2 = 0f;
float currentTimeBot3 = 0f;
float currentTimeBot4 = 0f;
float currentTimeBot5 = 0f;
float currentTimePlayer = 0f;
float bestTime = 0f;
bool first, second, third, startrace, finishrace;
public GameObject startCountDown;
private void Start()
{
//startrace = true;
StartCoroutine(waitForTheCountdown());
}
private void Update()
{
if (startrace)
{
timer += Time.deltaTime;
}
else
{
timer = 0f;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Bot1"))
{
Debug.Log("Bot1 Finished");
currentTimeBot1 = timer;
Debug.Log(currentTimeBot1.ToString());
}
if (other.gameObject.CompareTag("Bot2"))
{
Debug.Log("Bot2 Finished");
currentTimeBot2 = timer;
Debug.Log(currentTimeBot2.ToString());
}
if (other.gameObject.CompareTag("Bot3"))
{
Debug.Log("Bot3 Finished");
currentTimeBot3 = timer;
Debug.Log(currentTimeBot3.ToString());
}
if (other.gameObject.CompareTag("Bot4"))
{
Debug.Log("Bot4 Finished");
currentTimeBot4 = timer;
Debug.Log(currentTimeBot4.ToString());
}
if (other.gameObject.CompareTag("Bot5"))
{
Debug.Log("Bot5 Finished");
currentTimeBot5 = timer;
Debug.Log(currentTimeBot5.ToString());
}
if (other.gameObject.CompareTag("Player"))
{
Debug.Log("Player Finished");
currentTimePlayer = timer;
Debug.Log(currentTimePlayer.ToString());
}
}
private IEnumerator waitForTheCountdown()
{
yield return new WaitForSeconds(6f);
if (startCountDown.GetComponent<StartCountdown>().raceStarted == true)
{
startrace = true;
}
else
{
startrace = false;
}
}
我尝试过这种公式,但它太长并且只适用于某些情况
if(currentTimePlayer > currentTimeBot1 || currentTimePlayer > currentTimeBot2 || currentTimePlayer > currentTimeBot3 || currentTimePlayer > currentTimeBot4 || currentTimePlayer > currentTimeBot5){
Debug.Log("1st!");
}
最佳答案
我只是将它们全部存储在一个列表中(通常使用 if-else
甚至可能 switch-case
以获得更好的性能)
public List<GameObject> winners = new List<GameObject>();
private void OnTriggerEnter(Collider other)
{
switch(other.gameObject.tag)
{
case "Bot1":
Debug.Log("Bot1 Finished");
currentTimeBot1 = timer;
Debug.Log(currentTimeBot1.ToString());
break;
case "Bot2":
Debug.Log("Bot2 Finished");
currentTimeBot2 = timer;
Debug.Log(currentTimeBot2.ToString());
break;
case "Bot3":
Debug.Log("Bot3 Finished");
currentTimeBot3 = timer;
Debug.Log(currentTimeBot3.ToString());
break;
case "Bot4":
Debug.Log("Bot4 Finished");
currentTimeBot4 = timer;
Debug.Log(currentTimeBot4.ToString());
break;
case "Bot5":
Debug.Log("Bot5 Finished");
currentTimeBot5 = timer;
Debug.Log(currentTimeBot5.ToString());
break;
case "Player":
Debug.Log("Player Finished");
currentTimePlayer = timer;
Debug.Log(currentTimePlayer.ToString());
break;
default:
return;
}
// this is only matched if one of the before tags matched
winners.Add(other.gameObject);
}
这样您不仅可以获得第一个获胜者 (winners[0]
),而且实际上您可以获得他们完成比赛的整个顺序,例如
for(var i = 0; i< winners.Count; i++)
{
Debug.LogFormat("{0}. {1}", i + 1, winners[i].name);
}
如果您还需要完成时间,而不是使用像这样的字典
public Dictionary<GameObject, float> winners = new Dictionary<GameObject, float>();
private void OnTriggerEnter(Collider other)
{
switch(other.gameObject.tag)
{
case "Bot1":
winners.Add(other.gameObject, timer);
break;
}
// ...
Debug.Log(other.gameObject.tag + " Finished");
Debug.Log(winners[other.gameObject].ToString());
}
再次访问第一个,例如
var winner = winners[0].key;
var bestTime = winners[0].value;
或迭代所有获胜者
foreach(var kvp in winners)
{
var winner = kvp.key;
var finishTime = kvp.value;
//...
}
或访问特定游戏对象完成时间
var finishTime = winners[aCertainGameObject];
关于c# - 计算谁先冲过终点线 (C# UNITY),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683876/