检查一组对象的移动统一3D

问题描述 投票:2回答:1

我有相同的标签几个对象,我要检查,如果他们被移动或没有,打电话时,他们不动的功能。所以我使用的代码波纹管,但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);
    }
c# unity3d
1个回答
1
投票

写这样的代码

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; 
}
© www.soinside.com 2019 - 2024. All rights reserved.