c# - 计算谁先冲过终点线 (C# UNITY)

标签 c# unity-game-engine

我已经解决了计时器并记录了冲过终点线的人的时间。但我不知道如何计算谁是第一个冲过终点线的人。有人可以给我一些想法这是我的代码

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/

相关文章:

android - 来自 Unity 应用程序的 WebSocket 连接

c# - UnityARAlignment 在 Unity ARKit 插件中意味着什么?

c# - Unity 的默认脚本图标的位置在哪里

c# - 我的 ASP.NET 应用程序如何从 web.config 自动获取 SMTP 设置?

c# - 获取网格列的位置

c# - 按倒数第二个和最后一个之间的时间跨度观察延迟

ios - 尝试确定围绕某个点的运动矢量是顺时针还是逆时针

c# - XAML 找不到命名空间。有时

unity-game-engine - 我正在尝试检查我的子弹和敌人的统一碰撞,但它不起作用

performance - 对于动态游戏场上拥有大量 AI 的游戏来说,哪种导航方法最高效、最灵活?