Raycasting for audio detection 'echos'?

Hi all, I’m working on a 3D stealth game with an old ‘thief: deadly shadows’ feel and want a more in depth audio detection system than just a simple distance check - to stop guards hearing the player shuffling around when they’re only 2 feet away but through 2 feet of solid stone wall. I’d like to do this with a random ‘echoing’ raycast from the player.

What I’m picturing for ‘coding logic’ is to ‘hedgehog’ multiple raycasts randomly to reflect off walls until a distance equivalent to the player noise at that time has been reached. Every enemy hit with any of those rays has heard the player. if the player makes more noise then the raycast length is larger, simulating being heard from further away, although still unhearable from behind walls etc.

the attached image is a very simple 2d sketch of what I mean, if any part of an NPC was in the dark green area they would have been hit by a raycast and heard something, if not they would have missed it, despite both enemies being within the direct line of sight hearing range.

my question is this: is this a sensible way of achieving what I’m aiming at? For now it will only be the player that uses the ‘raycast hedgehog’ approach, NPC’s can still use distance for audio detection to keep performance loss to a minimum if it becomes a factor. This seems like it’ll be quite a lot of code for a relatively imperceivable change to gameplay, but occasionally it’ll break immersion without it.

Would it be significantly more performant to have a ‘noise absorption’ slider for all the walls, and modify a direct raycastAll between the player and enemy within a given ‘noise sphere’? this would be less ‘realistic’, but maybe not detectably so for a player?

or is there a better third way that I haven’t considered?

Hope this wasn’t too longwinded, thanks in advance,
Sundown

I think it might be simpler to use the NavMesh distance as a disqualifier…

        public bool CanMoveTo(Vector3 destination)
        {
            NavMeshPath path = new NavMeshPath();
            bool hasPath = NavMesh.CalculatePath(transform.position, destination, NavMesh.AllAreas, path);
            if (!hasPath) return false;
            if (path.status != NavMeshPathStatus.PathComplete) return false;
            if (GetPathLength(path) > maxNavPathLength) return false;

            return true;
        }

        private float GetPathLength(NavMeshPath path)
        {
            float total = 0;
            if (path.corners.Length < 2) return total;
            for (int i = 0; i < path.corners.Length - 1; i++)
            {
                total += Vector3.Distance(path.corners[i], path.corners[i + 1]);
            }

            return total;
        }

This particular bit of code is actually used in the RPG course to keep you from clicking too far away from your location, but it will also do a bit of what you’re seeing here. In example 1, the move distance might be… something like 10 units… but in example 2, even though the physical distance isn’t much bigger, the move distance will be, so you can use this to filter out the sound.

1 Like

Perfect, thanks Brian, I’ll give it a shot. I’m assuming I can have an audio navmesh ‘behind’ the standard npc navmesh that links meshes over a balcony, say, if the player makes a noise on building A’s balcony the guard 10 feet away across the road on Building B’s balcony will hear it but know they can’t ‘fly’ between balconies?

would this be the best way of linking mesh areas?

thanks for your help!

For multiple NavMeshes, you’ll need the NavMesh Tools off of Git Hub https://github.com/Unity-Technologies/NavMeshComponents
My idea basically just uses the existing NavMesh… the more twists and turns there are in a path, the “longer” it is, which approximates what would also happen with audio. Remember, for most games, “close enough” is usually “good enough”. The more raycasts and extra calls you put in the game, the more of a hit you’ll take on the frame rate.

Perfect, thank you Brian, I’ll give it a crack. Have a great day!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms