Example #1
0
        void Start()
        {
            dissolve = GetComponent <MeshRenderer>().material;
            dissolve.SetFloat("_DissolveAmount", 1f);

            volume             = GetComponentInChildren <SLiquid_Volume>();
            volume.liquidLevel = 0f;
        }
        protected void Update()
        {
            if (isEmitting)
            {
                splashParticles.startColor = liquidColor;
                // Raycast straight down from liquid dispenser and set the particle lifetime accordingly
                liquidJetRay.origin = transform.TransformPoint(spillParticleSystem.shape.position);
                if (Physics.Raycast(liquidJetRay, out liquidJetImpact, Mathf.Infinity, layerMask, QueryTriggerInteraction.Collide))
                {
                    float distance = liquidJetImpact.distance;
                    // Did we hit a mug? Check if the point is inside and fill it
                    Transform root = liquidJetImpact.collider.transform.root;
                    if (root.CompareTag("Vessel"))
                    {
                        SLiquid_Volume volume       = root.GetComponentInChildren <SLiquid_Volume>();
                        Collider       volumeInside = volume.GetComponent <Collider>();
                        if (CheckInsideCollider(volumeInside, liquidJetImpact.point))
                        {
                            Color vesselLiquidColor = volume.Fill(fillRate * Time.deltaTime, liquidColor);

                            // If the vessel is not empty raycast against the liquid level plane and update distance if point is still inside the mug
                            if (!volume.IsEmpty())
                            {
                                if (volume.cutPlane.Raycast(liquidJetRay, out float planeDistance) && CheckInsideCollider(volumeInside, liquidJetRay.GetPoint(planeDistance)))
                                {
                                    distance = planeDistance;
                                    // We hit the liquid vessel so update the color of generated splashes
                                    splashParticles.startColor = vesselLiquidColor;
                                }
                            }
                        }
                    }
                    // Set the startLifetime for particles so that they expire at the impact point
                    // t = SQRT(2 * s / a)
                    spillParticles.startLifetime = Mathf.Sqrt((2f * distance) / (Physics.gravity.magnitude * spillParticles.gravityModifierMultiplier));
                }
                // Emit particles with new lifetime set
                if (!spillParticleSystem.isPlaying)
                {
                    // Make sure we emit at least some particles for short bursts when spilling on activation
                    spillParticleSystem.Emit((int)(spillParticleSystem.emission.rateOverTime.constant / 2f));
                    spillParticleSystem.Play();
                }
            }
        }