Hi is their a way to get the transforms of an audio source so that if i walk behind my ai it will hear the audio and move towards the audios postion
An AudioSource is just a component so it has a transform reference to the Transform of the object it is attached to.
Make a class than keeps track of the active enemies on scene.
public class EnemyManager:MonoBehaviour{
private List<Transform>list = new List<Transform>();
public void AddEnemy(Enemy enemy){list.Add(enemy.transform);}
public void RemoveEnemy(Enemy enemy){list.Remove(enemy.transform);}
public Transform[] GetTransformEnemies{return list.ToArray();}
}
Every time you activate/create/Destroy an enemy, you also called the appropriate method.
Now when you play a sound:
private AudioSource audioSource = null;
private EnemyManager enemyManager = null;
private Transform player = null;
void Start(){
// Get audio source, player transform and enemy manager
}
private void PlaySound(AudioClip clip, Action<Transform> action){
// Play the sound
Transform[] enemies = enemyManager.GetTransformEnemies();
foreach(Transform enemy in enemies){
float distance = Vector3.Distance(enemy.position,player.position );
float range = audioSource.maxDistance
if(distance < range){
// Enemy heard
action(player.transform);
}
}
}