99 lines
2.2 KiB
C#
99 lines
2.2 KiB
C#
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class SoundArea : MonoBehaviour
|
|
{
|
|
public string soundName = string.Empty;
|
|
|
|
public bool sound3d = true;
|
|
|
|
public GameObject soundOrigin;
|
|
|
|
public float volume = 1f;
|
|
|
|
public bool activeNormal = true;
|
|
|
|
public bool activeIce = true;
|
|
|
|
[HideInInspector]
|
|
public AudioObject audioObject;
|
|
|
|
[Header("Area")]
|
|
public Transform testCollisionObject;
|
|
|
|
public Collider collider;
|
|
|
|
public LayerMask rayMask = default(LayerMask);
|
|
|
|
private RaycastHit raycastHit = default(RaycastHit);
|
|
|
|
public float maxDistance = 50f;
|
|
|
|
public float minVolume;
|
|
|
|
public FishingPlayer fishingPlayer;
|
|
|
|
[ReadOnly]
|
|
public float currentDistance;
|
|
|
|
[HideInInspector]
|
|
public SoundSettings soundSettings;
|
|
|
|
public void Start()
|
|
{
|
|
if ((bool)GameController.Instance)
|
|
{
|
|
if ((GameController.Instance.iceLevel && !activeIce) || (!GameController.Instance.iceLevel && !activeNormal))
|
|
{
|
|
return;
|
|
}
|
|
fishingPlayer = GameController.Instance.fishingPlayer;
|
|
}
|
|
if ((bool)GlobalSettings.Instance)
|
|
{
|
|
soundSettings = GlobalSettings.Instance.soundSettings;
|
|
}
|
|
if (soundOrigin == null)
|
|
{
|
|
soundOrigin = base.gameObject;
|
|
}
|
|
if (sound3d)
|
|
{
|
|
audioObject = AudioController.Play(soundName, soundOrigin.transform, volume);
|
|
}
|
|
else
|
|
{
|
|
audioObject = AudioController.Play(soundName, volume);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if ((bool)collider && (bool)fishingPlayer && (bool)audioObject)
|
|
{
|
|
Vector3 vector = collider.ClosestPointOnBounds(fishingPlayer.transform.position);
|
|
if (rayMask.value != 0 && Physics.Raycast(fishingPlayer.transform.position, vector - fishingPlayer.transform.position, out raycastHit, 666f, rayMask.value))
|
|
{
|
|
vector = raycastHit.point;
|
|
}
|
|
if ((bool)testCollisionObject)
|
|
{
|
|
testCollisionObject.position = vector;
|
|
}
|
|
currentDistance = Vector3.Distance(fishingPlayer.transform.position, vector);
|
|
if (currentDistance > maxDistance)
|
|
{
|
|
audioObject.volume = minVolume;
|
|
}
|
|
else
|
|
{
|
|
audioObject.volume = Mathf.Lerp(minVolume, volume, 1f - currentDistance / maxDistance);
|
|
}
|
|
if ((bool)GlobalSettings.Instance)
|
|
{
|
|
audioObject.volume *= soundSettings.GetVolume(SoundSettings.SoundCategory.AMBIENT);
|
|
}
|
|
}
|
|
}
|
|
}
|