Esempio n. 1
0
        public void FixedUpdate()
        {
            collidedAttractor     = null;
            playerCollidesWithAny = false;
            foreach (Attractor a in attractors)
            {
                if (Physics2D.OverlapCircleAll(a.center, a.pullRadius, 1 << LayerMask.NameToLayer("Player")).Length > 0)
                {
                    // player is inside any attractor
                    collidedAttractor     = a;
                    playerCollidesWithAny = true;
                    break;
                }
            }

            if (playerCollidesWithAny && collidedAttractor == this)
            {
                Vector2 forceDirection = Vector3.up;
                float   dist           = 1000F;
                if (playerRb != null)
                {
                    // calculate direction from player to center of this
                    forceDirection = center - new Vector2(Player._instance.transform.position.x, Player._instance.transform.position.y);

                    // calculate distance to the center of this
                    dist = Mathf.Abs(Vector3.Distance(Player._instance.transform.position, transform.position));

                    // calculate an amplifier value based on the distance to the center
                    float distAmp = Mathf.InverseLerp(pullRadius, 0, dist);

                    Debug.Log("distAmp " + distAmp);

                    // apply force on player towards center of this
                    playerRb.AddForce(forceDirection.normalized * maxPullForce * Time.fixedDeltaTime * pullAmplifier * distAmp);
                }

                // update shader
                attractorMaterial.SetFloat("_AttractorRadius", pullRadius);
                Vector3 localPos = transform.InverseTransformPoint(transform.position);
                localPos.z = transform.position.z;
                attractorMaterial.SetVector("_AttractionCenter", localPos);
                attractorMaterial.SetFloat("_PlayerDistance", dist);

                // update camera shake
                float shake = Mathf.InverseLerp(pullRadius, 0, dist);
                CamShake.attractorDistance = shake;
                SoundPlayer.rumbleAlive    = true;
                SoundPlayer.rumbleVolume   = shake;
            }

            if (playerCollidesWithAny == true && shaking == false && collidedAttractor == this)
            {
                SoundPlayer.rumbleAlive = true;
                SoundManager.PlayRumbleSound(transform.position);
                CamShake.AttractorShake();
                shaking = true;
            }
            else if (playerCollidesWithAny == false)
            {
                SoundPlayer.rumbleAlive = false;
                CamShake.AttractorShakeBreak();
            }
        }