zoukankan      html  css  js  c++  java
  • 根据声音获取对象

    // 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;
        }
  • 相关阅读:
    [20190415]10g下那些latch是共享的.txt
    [20190415]11g下那些latch是共享的.txt
    [20190409]pre_page_sga=true与连接缓慢的问题.txt
    [20190402]Library Cache mutex.txt
    scrapy简单使用方法
    PHP多进程系列笔(转)
    RdKafka文档翻译
    python判断字符串中是否包含子字符串
    python 逐行读取txt文件
    redis使用watch完成秒杀抢购功能(转)
  • 原文地址:https://www.cnblogs.com/88999660/p/3732313.html
Copyright © 2011-2022 走看看