Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        // 清空
        for (int i = 0; i < Gravitys.Length; i++)
        {
            Gravity gravity = Gravitys[i];

            gravity.ClearForce();
        }

        // 每两个物体之间加引力
        for (int i = 0; i < Gravitys.Length; i++)
        {
            Gravity gravity = Gravitys[i];

            for (int j = i + 1; j < Gravitys.Length; j++)
            {
                Gravity gravity2 = Gravitys[j];

                Vector3 dir   = gravity2.transform.position - gravity.transform.position;
                Vector3 force = gravity.Mass * gravity2.Mass * dir * Mathf.Pow(Vector3.SqrMagnitude(dir), 2 / 3) * Scale;

                gravity.AddForce(force);
                gravity2.AddForce(-force);
            }
        }

        // 触发update
        for (int i = 0; i < Gravitys.Length; i++)
        {
            Gravity gravity = Gravitys[i];

            gravity.LetUpdate();
        }
    }