예제 #1
0
#pragma warning restore 0414

    // Use this for initialization
    protected virtual void Start()
    {
        m_rigidBody = GetComponent <Rigidbody>();

        m_boxCollider = transform.GetComponentInChildren <BoxCollider>();

//         m_previousPos.Add(Vector3.zero);
//         m_previousPos.Add(Vector3.zero);

        for (int i = 0; i < transform.childCount; ++i)
        {
            Transform    tmpTransform    = transform.GetChild(i);
            MeshRenderer tmpMeshRenderer = tmpTransform.GetComponent <MeshRenderer>();
            if (tmpMeshRenderer == null)
            {
                continue;
            }

            if (tmpTransform.gameObject.name.Contains("Base"))
            {
                m_base     = tmpTransform;
                m_baseSize = tmpMeshRenderer.bounds.size;
            }
            m_size += tmpMeshRenderer.bounds.size;
        }

        float volume = MeshVolumeHelper.VolumeOfObject(gameObject);

        // 2700 is the average density of a rock Cf. http://www.les-mathematiques.net/phorum/read.php?2,49845
        m_rigidBody.mass = volume * 2700;
    }
    void updateSize()
    {
        m_size = Vector3.zero;

        MeshRenderer meshRenderer = GetComponent <MeshRenderer>();

        if (meshRenderer)
        {
            m_size = meshRenderer.bounds.size;
        }
        else
        {
            for (int i = 0; i < transform.childCount; ++i)
            {
                meshRenderer = transform.GetChild(i).GetComponent <MeshRenderer>();
                if (!meshRenderer)
                {
                    continue;
                }

                m_size += meshRenderer.bounds.size;
            }
        }

        float volume = 0;

        if (GetComponent <SphereCollider>())
        {
            volume = Mathf.PI * Mathf.Pow(GetComponent <SphereCollider>().radius, 3) * 4 / 3;
        }
        else
        {
            volume = MeshVolumeHelper.VolumeOfObject(gameObject);
        }
        m_rigidBody.mass = volume * m_density;
    }