Hi there! Does anyone know how to stop this script (EnnemyPlaySound
)…after a distance from the camera, so that it stops playing its audio sources’ sound clips… it is script that uses the animator animations as ‘playing/controlling/driver’ for the sound clips to play; thus, is a script on a monster/enemy character…the character plays the sounds clips as it goes from animation to the next (in the animator list); I tried many things, like GetComponent
to disable the Component
/MonoBehaviour
/script, by the distance… it just does not work; also tried by ‘collider’, the character’s script is turned off…and as the characters walks to and ‘enters’ the camera’s collider…the script would be turned on…by the collider on the camera (the monster ‘colliding’ with the camera collider; thus, activating the script upon colliding camera; I put a collider on the camera that i linked to the script on the monster, I tried ‘is trigger’ on the collider of camera…and not; both does not work). Didn’t work neither.
Tried many things, either nothing happens or the script just does not allow to be altered… I’m at loss as to what to do next…to make it work; it’s simple, it’s just the script to turn off at a distance from camera - if only it could be just a click…can’t do it. Some finnicky fickle thing (yes, I know that computers are like that - 0s and 1s…and no in betweens; such is ‘code’ - 'ultra-formal/syntaxic … to a ‘period(.) missing’; you have to ‘tell’ everything to the script/computer…because computer doesn’t know (it’S what AI is the next revolution for coding/ChaptGPT))…I tried several assets that turn on/off audio sources (on asset store), don’t work. Script does not want to comply. I know it’s the script the problem, because the sounds turns off - immediately when I ‘tick’ off the script when I play ‘live’…so it’s the scrip the problem, with the audio. The goal is simple, it is that the script is ‘turned off’ ‘Far away’ from the camera (the monsters don’t produce any sfx from afar…)…as they close-in, the script reaches the ‘area’ of the camera/or the camera itself (with the ‘Listener’ on it) and this area triggers the ‘activation’ of the script…and hten the monster’s script ‘now On’ starts playing the monster sounds (at that distance/colliding area/zone that triggers the script to play - only there; so it is a zone that ‘surrounds’ the camera/player…and monster walks to it…and script starts; outside that zone, the script is/stays Off)… Any help is greatly appreciated and thank you very much in advance (am not programmer).
PS: I am doing this, and not using the (more obvious/simple) ‘audio source spatial blend’/Min Distance/Max Distance ‘linear’ volume reduction (With distance) - on the Audio Sources themselves that play the sound clips…because I use *2D audio sources/no spatial blend (instead of 3D audio sources, that use the Spatial Blend…to lower ‘the volume’ by distance…I don’t use this, because, I noticed that 3d spatial blend lowers the quality of a HQ/HD sound effect (I use FLAC and it shows/sounds different) vs a 2D audio source …that plays the same volume level anywhere…so I was like, great, I lose the ‘depth’ of 3D spatial blend, am stuck with 2D audio sources that ‘play same volume’ anywhere in the 3d space (am doing a 3D FPS game that uses the 360o 3d space)…but 2d audio sources/no blend Keep the quality of the sound clip, exactly (no filtering/no processing of it), as how it is (unlike, the 3D spatial blend…that ‘filters/processes’, muffles, degrades and lowreses the soundclip; though, it’s subtle, many would be fooled to think it’s the same exact sound; but, upon really listening, several times, there is a difference (lowering) in audio quality; basically, the sound sounds more 3D/spatial blend…but lowresed/blurred/at a loss of quality/sounds ‘processed’ (obviously, it would be better - processed (because 3D spatial blend…is a process…) but does not ‘sound processed’…thus, a 3D blend that does not reduce the quality of the sound clip once 3d processed; is seamless/no difference whatsoever, but now obtains the 3d depth/volume fading by 3d distance). I tried a 2D volume fading and it worked…but not with this script; even 2d audio source volume fading by distance (Vector3, distance to listener) muffed the sound clip because the sound ‘lowered’ in volume (thus the 2d volume processing, was enough to reduce quality of sound clip); only if I ‘cut’ the sound (like ‘popping/clipping’ the sound) and ‘stopped’ the sound clip/audio source…now the sound clip was completely untounched/unprocessed — just turned off/on, ‘at a certain distance’. So, it worked, but, when I tried to combine/add that script…to this one here…nothing happens…it demonstrates this script here decides and I am not sure what it is exactly; I did try to ‘add’ the content of that 2d volume script – to this one here…didn’t work neither.
Here is the EnnemyPlaySound
script / credit: Sustavov Vyacheslav
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPlaySound : MonoBehaviour
{
public static int numAnimation = 1;
public Animator animator;
[System.Serializable]
public struct AnimationList
{
public string animationName;
public float percentStart;
public AudioSource audioForAnimation;
}
public AnimationList[] animList = new AnimationList[numAnimation];
void Start(){
animator = GetComponent<Animator> ();
}
void PlaySound ()
{
foreach (AnimationList nAnim in animList) {
if (animator.GetCurrentAnimatorStateInfo (0).IsName (nAnim.animationName)) {
int i = (int)animator.GetCurrentAnimatorStateInfo (0).normalizedTime;
if (animator.GetCurrentAnimatorStateInfo (0).normalizedTime >= i + nAnim.percentStart && animator.GetCurrentAnimatorStateInfo (0).normalizedTime <= i + nAnim.percentStart + 0.1) {
if (i >= 0 && animator.GetCurrentAnimatorStateInfo (0).loop) {
nAnim.audioForAnimation.Play ();
}
if (i == 0 && !animator.GetCurrentAnimatorStateInfo (0).loop) {
nAnim.audioForAnimation.Play ();
}
}
}
if (animator.GetNextAnimatorStateInfo (0).IsName (nAnim.animationName)) {
int j = (int)animator.GetNextAnimatorStateInfo (0).normalizedTime;
if (animator.GetNextAnimatorStateInfo (0).normalizedTime >= j + nAnim.percentStart && animator.GetNextAnimatorStateInfo (0).normalizedTime <= j + nAnim.percentStart + 0.1) {
nAnim.audioForAnimation.Play ();
}
}
}
}
void FixedUpdate ()
{
PlaySound ();
}
}
Here is the 2D volume sound script / credit: John Leonard French
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Volume2D : MonoBehaviour
{
public Transform listenerTransform;
public AudioSource audioSource;
public float minDist=1f;
public float maxDist=400f;
void Update()
{
float dist = Vector3.Distance(transform.position, listenerTransform.position);
if(dist < minDist)
{
audioSource.volume = 1;
}
else if(dist > maxDist)
{
audioSource.volume = 0;
}
else
{
audioSource.volume = 1 - ((dist - minDist) / (maxDist - minDist));
}
}
}
Here is another one that ‘gets’ the script’ and turns it off/on - by collider entry, couldn’t make it work neither. / credit: Trifluvious
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Mute : MonoBehaviour{
public Collider[] YourCachedCollidersArray;
void Start() {
YourCachedCollidersArray = Physics.OverlapSphere(transform.position, 15f);
EnemyPlaySound enemyplaysound = GetComponent<EnemyPlaySound>();
}
void OnTriggerEnter(Collider hit) {
foreach(Collider c in YourCachedCollidersArray) {
c.gameObject.GetComponent<EnemyPlaySound>().enabled = true;
}
}
void OnTriggerExit(Collider hit) {
foreach(Collider c in YourCachedCollidersArray) {
c.gameObject.GetComponent<EnemyPlaySound>().enabled = false;
}
}
}
The only way (I see/think) is through that first script ‘EnemyPlaySound’ that is driven by the animator/animations, that play a sound clip as the enemy moves/animates, the code changes should? be on it, specifically, to affect the sound by distance. It only works on it, because when I turn it off, now it works, and the sound stops playing; or if turned on, the sound clips play again; showing it is the responsible/controlling script (not the others) that - from what/which script the sound comes from.