/// <summary>
        /// Finds the IAudioInfluencer objects that are to be applied to the audio source.
        /// </summary>
        /// <returns>Collection of IAudioInfluencers between the user and the game object.</returns>
        private List<IAudioInfluencer> GetInfluencers()
        {
            List<IAudioInfluencer> influencers = new List<IAudioInfluencer>();
            Transform cameraTransform = CameraCache.Main.transform;

            // Influencers take effect only when between the emitter and the user.
            // Perform a raycast from the user toward the object.
            Vector3 direction = (gameObject.transform.position - cameraTransform.position).normalized;
            float distance = Vector3.Distance(cameraTransform.position, gameObject.transform.position);

            int count = UnityPhysics.RaycastNonAlloc(cameraTransform.position,
                                                direction,
                                                hits,
                                                distance,
                                                UnityPhysics.DefaultRaycastLayers,
                                                QueryTriggerInteraction.Ignore);
            
            for (int i = 0; i < count; i++)
            {
                IAudioInfluencer influencer = hits[i].collider.gameObject.GetComponentInParent<IAudioInfluencer>();
                if (influencer != null)
                {
                    influencers.Add(influencer);
                }
            }

            return influencers;
        }
예제 #2
0
        /// <summary>
        /// Finds the IAudioInfluencer objects that are to be applied to the audio source.
        /// </summary>
        /// <returns>Collection of IAudioInfluencer objects.</returns>
        private List <IAudioInfluencer> GetInfluencers()
        {
            List <IAudioInfluencer> influencers = new List <IAudioInfluencer>();

            // For influencers that take effect only when between the emitter and the user, perform a raycast
            // from the user toward the object.
            Vector3 direction = (gameObject.transform.position - Camera.main.transform.position).normalized;
            float   distance  = Vector3.Distance(Camera.main.transform.position, gameObject.transform.position);

            int count = Physics.RaycastNonAlloc(Camera.main.transform.position,
                                                direction,
                                                hits,
                                                distance,
                                                Physics.DefaultRaycastLayers,
                                                QueryTriggerInteraction.Ignore);

            for (int i = 0; i < count; i++)
            {
                IAudioInfluencer ai = hits[i].collider.gameObject.GetComponentInParent <IAudioInfluencer>();
                if (ai != null)
                {
                    influencers.Add(ai);
                }
            }

            return(influencers);
        }