文章出處

// Cast a sphere with the desired radius. Check each object's audio source to see if audio is playing. If audio is playing
    // and its audibility is greater than the audibility threshold then return the object heard
    /// <summary>
    /// Withins the hearing range.
    /// </summary>
    /// <returns>The hearing range.</returns>
    /// <param name="transform">玩家</param>
    /// <param name="linearAudibilityThreshold">Linear audibility threshold.</param>
    /// <param name="hearingRadius">可以聽到的范圍</param>
    /// <param name="objectLayerMask">對象層遮罩</param>
    public static Transform WithinHearingRange(Transform transform, float linearAudibilityThreshold, float hearingRadius, LayerMask objectLayerMask)
    {
        Transform objectHeard = null;
        var hitColliders = Physics.OverlapSphere(transform.position, hearingRadius, objectLayerMask);//獲取范圍內的對象
        if (hitColliders != null) {
            float maxAudibility = 0;
            AudioSource colliderAudioSource;
            for (int i = 0; i < hitColliders.Length; ++i) {
                // Check to see if the hit agent has an audio source and that audio source is playing
                if ((colliderAudioSource = hitColliders[i].GetComponent<AudioSource>()) != null && colliderAudioSource.isPlaying) {//對象發出聲音
                    // The audio source is playing. Make sure the sound can be heard from the agent's current position
                    var audibility = colliderAudioSource.volume / Vector3.Distance(transform.position, hitColliders[i].transform.position);//聲音衰減
                    if (audibility > linearAudibilityThreshold) {//聲音大于衰減閥值
                        if (audibility > maxAudibility) {//獲取聲音最大的那個
                            maxAudibility = audibility;
                            objectHeard = hitColliders[i].transform;
                        }
                    }
                }
            }
        }
        return objectHeard;
    }

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()