Beispiel #1
0
    void OnCollisionEnter2D(Collision2D collision)
    {
        GameObject obj = collision.collider.gameObject;

        if (obj)
        {
            Damageable  objDMG = obj.GetComponent <Damageable>();
            Rigidbody2D objRB  = obj.GetComponent <Rigidbody2D>();

            if (objDMG && objRB)
            {
                float acceleration = (objRB.velocity - objDMG.lastVelocity).magnitude / Time.fixedDeltaTime;

                BreakableObject br = obj.GetComponent <BreakableObject>();
                if (br)
                {
                    if (br.GetArea() < 5f)
                    {
                        return;
                    }
                    else
                    {
                        Rigidbody2D rb = GetComponent <Rigidbody2D>();
                        if (rb)
                        {
                            Vector3 impactPoint = transform.position - collision.collider.transform.position;
                            Vector3 finalPoint  = impactPoint + (Vector3)rb.velocity;
                            br.Adjust(impactPoint, finalPoint, acceleration * objRB.mass);
                        }
                    }
                }

                float damage = acceleration / objRB.mass * damageFactor;

                if (damage >= 1f)
                {
                    //Debug.Log(name + " damaged " + objDMG.name + " by " + damage);
                    objDMG.Damage(damage);
                }
            }
        }
    }
    public void GenerateAsteroid(bool removeAfter = true)
    {
        if (sides < 3)
        {
            sides = 3;
        }

        Vector3[] vertices = GenerateVertices(GenerateAngles());
        GetComponent <MeshFilter>().mesh          = GenerateMesh(vertices);
        GetComponent <PolygonCollider2D>().points = Array.ConvertAll(vertices, v => (Vector2)v).ToArray();

        BreakableObject bo = GetComponent <BreakableObject>();

        if (bo)
        {
            float thickness = GetComponent <MeshRenderer>().material.GetFloat("_OutlineThickness") / Mathf.Clamp(Mathf.Sqrt(bo.GetArea() * 0.25f), 1f, 100f);
            GetComponent <MeshRenderer>().material.SetFloat("_OutlineThickness", thickness);
        }

        if (removeAfter)
        {
            Destroy(this);
        }
    }