簡體   English   中英

檢查一組對象是否正在移動 Unity 3D

[英]Check if a set of objects is moving Unity 3D

我有幾個具有相同標簽的對象,我想檢查它們是否在移動,以便在它們不移動時調用函數。 所以我使用了下面的代碼,但是即使某些對象仍在移動, aremoving 始終是錯誤的! 你知道我的代碼有什么問題嗎?

腳本:

bool aremoving;

void LateUpdate()
    {
        GameObject[] Cubes = GameObject.FindGameObjectsWithTag("Cube");
        foreach (GameObject Cube in Cubes)
        {
            if (Cube.GetComponent<Rigidbody>() == null)
            {
                continue;
            }
            if (Cube.GetComponent<Rigidbody>().velocity.magnitude > 0.01)
            {
                aremoving = true;
            }
            if (Cube.GetComponent<Rigidbody>().velocity.magnitude <= 0.01)
            {
                aremoving = false;
            }
        }
        Debug.Log("Cubes moving: " + aremoving);
    }

像這樣寫代碼

bool aremoving;

void LateUpdate()
{
    GameObject[] Cubes = GameObject.FindGameObjectsWithTag("Cube");
    foreach (GameObject Cube in Cubes)
    {
        if (Cube.GetComponent<Rigidbody>() == null)
        {
            continue;
        }
        if (Cube.GetComponent<Rigidbody>().velocity.magnitude > 0.01f)
        {
            aremoving = true;
        }
    Debug.Log("Cubes moving: " + aremoving);
    aremoving  = false; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM