自己手动实现左右耳的声音区别
人之所以能通过听到的声音辨别音源的方向,主要就是因为左右耳的双重信号接收。
在左边的声音到达左耳会比到达右耳要快一些,同时,频率越高的音段会被头部遮挡一部分,导致右耳听到的声音要比左耳听到的声音小。
除此之外,人耳的形状实际上是一个滤波器,当声音从正面传来时,可以几乎无阻碍的被我们接收,而背面的则会被耳廓遮挡,相当于一个低通滤波器,我们就依据这个来编写脚本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioStereo : MonoBehaviour
{
public GameObject leftPlayer;
public GameObject rightPlayer;
public Transform mainCamera;
public Transform leftEarPos;
public Transform rightEarPos;
public float strength = 0.3f;
public float reduction = 0.4f;
public float delayTimePerUnit = 0.02f;
private AudioSource leftStereo;
private AudioSource rightStereo;
private AudioLowPassFilter leftLowPass;
private AudioLowPassFilter rightLowPass;
private float standardVolume = 0.5f;
private Vector3 relativePos;
private float degree;
private float lastDegree;
private bool ifLeft