Exemple #1
0
    // Update is called once per frame
    public void AddSplatter(Transform trn, Collision collision, Color color)
    {
        SplatterScript splatter = Instantiate(splatterPrefab);

        splatter.transform.position = trn.position;
        splatter.Init(color, true);
        bool    willFit   = true;
        float   splatSize = 2f;
        Vector3 pos       = collision.contacts[0].point;
        Vector3 normal    = collision.contacts[0].normal;

        if (normal == collision.collider.transform.right || normal == -collision.collider.transform.right)
        {
            float diff = Mathf.Abs(Vector3.Dot(pos - collision.collider.transform.position, collision.collider.transform.up));
            float max  = (collision.collider.transform.localScale.y - splatSize) / 2;
            if (diff > max)
            {
                willFit = false;
            }
        }
        else if (normal == collision.collider.transform.up || normal == -collision.collider.transform.up)
        {
            float diff = Mathf.Abs(Vector3.Dot(pos - collision.collider.transform.position, collision.collider.transform.right));
            float max  = (collision.collider.transform.localScale.x - splatSize) / 2;
            if (diff > max)
            {
                willFit = false;
            }
        }
        else
        {
            willFit = false;
        }

        if (willFit)
        {
            SplatScript splat = Instantiate(splatPrefab);
            splat.Init(collision.transform, normal, pos + normal * 0.001f, color, 2f);
        }

        splatterList.Add(splatter);
        while (splatterList.Count > 100)
        {
            splatterList[0].Remove();
        }
    }
Exemple #2
0
    void OnParticleCollision(GameObject other)
    {
        if (!collide)
        {
            return;
        }
        int numCollisionEvents = ps.GetCollisionEvents(other, collisionEvents);
        int i = 0;

        while (i < numCollisionEvents)
        {
            bool  willFit   = true;
            float splatSize = Random.Range(0.1f, 0.5f);

            Vector3 pos        = collisionEvents[i].intersection;
            Vector3 offset     = pos - other.transform.position;
            Vector3 normal     = collisionEvents[i].normal;
            Vector3 correction = Vector3.zero;

            if (normal == other.transform.right || normal == -other.transform.right)
            {
                float diff = Mathf.Abs(Vector3.Dot(pos - other.transform.position, other.transform.up));
                float max  = (other.transform.localScale.y - splatSize) / 2;

                correction = normal * (Vector3.Dot(offset, normal) - other.transform.localScale.x / 2);
                if (diff > max)
                {
                    willFit = false;
                }
            }
            else if (normal == other.transform.up || normal == -other.transform.up)
            {
                float diff = Mathf.Abs(Vector3.Dot(pos - other.transform.position, other.transform.right));
                float max  = (other.transform.localScale.x - splatSize) / 2;
                correction = normal * (Vector3.Dot(offset, normal) - other.transform.localScale.y / 2);
                if (diff > max)
                {
                    willFit = false;
                }
            }
            else if (normal == other.transform.forward || normal == -other.transform.forward)
            {
                float diffY = Mathf.Abs(Vector3.Dot(pos - other.transform.position, other.transform.up));
                float maxY  = (other.transform.localScale.y - splatSize) / 2;
                if (diffY > maxY)
                {
                    willFit = false;
                }
                float diffX = Mathf.Abs(Vector3.Dot(pos - other.transform.position, other.transform.right));
                float maxX  = (other.transform.localScale.x - splatSize) / 2;
                correction = normal * (Vector3.Dot(offset, normal) - other.transform.localScale.z / 2);
                if (diffX > maxX)
                {
                    willFit = false;
                }
            }
            else
            {
                willFit = true;
            }

            if (willFit)
            {
                SplatScript splat = Instantiate(splatPrefab);
                splat.Init(other.transform, normal, pos - correction + normal * 0.001f, color, splatSize);
            }

            i++;
        }
    }