- Home /
Enemy AI Hearing System
Hello there, im making a stealth zombie game and i've done almost anything but stealth mechanics. There are no tutorials such as Enemy Hearing system etc. I've done something with layermask and colliders, attached sphere collider to zombie and float called soundintensity, that's working PERFECTLY BUT WHEN SPHERE COLLIDER IS NOT SET AS TRIGGER!! I need it as trigger because player need to get close to zombie and that collider is kinda big so can anyone help me?? Code:
public void Follow()
{
Collider[] zombies = Physics.OverlapSphere(this.transform.position, soundIntensity, zombieLayer);
for (int i = 0; i < zombies.Length; i++)
{
zombies[i].GetComponent<Enemy>().iSeeYou = true;
}
}
thank you in advance guys!! i really need to get this done :D
Answer by BastianUrbach · Apr 18, 2018 at 06:37 PM
It seems like you can specify whether trigger hits should be included in a fourth parameter of OverlapSphere (use QueryTriggerInteraction.Collide). I haven't tried it though.
Answer by melsy · Apr 19, 2018 at 03:49 AM
Refer to my answer here for what i believe to be a better system.
Answer by TheBootlegger · Jan 09 at 05:36 PM
In case you're still looking for a solution to this, here's how I've implemented it using C# events:
using UnityEngine;
public class Noise
{
public delegate void OnNoise(Vector3 position, float distance);
public static event OnNoise SoundEvent;
//invoke the event with this void is to make sure that the event isn't invoked if it has no listeners
public static void MakeNoise(Vector3 Position, float Radius)
{
if (SoundEvent != null)
{
Debug.Log($"Noise made at {Position} with a radius of {Radius}");
SoundEvent.Invoke(Position, Radius);
}
else Debug.Log($"SoundEvent wasn't invoked, as there are no listeners");
}
}
in the enemy script, you need to have a method that has the same parameters as the OnNoise delegate void to subscribe to the event:
using UnityEngine
public class Enemy : MonoBehaviour
{
void Start()
{
//Subscribe to the event
Noise.SoundEvent += OnHearNoise;
}
void OnDestroy()
{
//Unsubscribe from the event
Noise.SoundEvent -= OnHearNoise;
}
void OnHearNoise(Vector3 Position, float radius)
{
//Do stuff...
}
}