#Playing sound events

1 messages · Page 1 of 1 (latest)

umbral merlin
#

What were those event actions going to be subscribed too? Is the trigger in question like an area that you could move around inside of? You could have an event subscribed to your controller actions, like fire an event on player move that evaluates if inside trigger area to decide if to play or not. Subscribe another event to player stop move to stop audio playback. Then I suppose if you need to handle a condition where player is already moving and crosses a trigger threshold you could subscribe the evaluation to that again.

#

Alternatively if performance isn’t really an issue in your case you could just evaluate if inside trigger or something every update. Ideally though when your trying to handle a game event you want to use events and subscribers to accommodate them. It’s the observer pattern and would help you add logic later on. Like say you need to force crouch on your player controller when enter trigger for first time, you could just add another subscriber to that event your already handling and both events wouldn’t have to know about each other.

#

Basically you want your systems/objects to announce something happened, and other systems (like your audio manager?) determine what to do with them. That way you could have like say 10 triggers in your case all creating the same event, and your audio manager might just be subscribing to them all and responding in the same way. Later on if you have different triggers (say uh.. different types of trigger areas per region/level) you could pass along the region/level as well on the event and have your subscriber take them in if they want. Your audio manager could select a different sound knowing it’s a different region for example and your force crouch function would still work and not need to change. (Ideally anyway). Not sure if I’m interpreting your question correctly or not.

haughty geode
umbral merlin
#

I’m not quite sure on your logic, but it seems like you want the OnTriggerEnter and such functions to be delegates and you want like an object collider on the swamp to call them. (Or call them via whatever is detecting the player is in swamp?)

#

So in your case the event might be that player collided with swamp object and you have that event adding those methods you have as delegates to execute on that event.

#

You basically want some sort logic that is identifying or detecting the swamp and character are interacting (I’m thinking a collider check would work well?) and then if that check passes you invoke the event, which would then execute any functions you have subscribed to it (in your case probably execute a function that does the movement check and then decide if to play audio etc. I’d probably have the audio play function obtain a reference to your player controller to check for movement or something like that.

#

I wouldn’t recommend using this example for opening doors but it’s a great way to explain using an event system. https://youtu.be/gx0Lt4tCDE0

In this video we take a look at how to build a Custom Event System in Unity. We look at why a Custom Event System might be useful for your game, and the advantages that building one might have over using singletons, or relying on dependencies in the inspector.

Be sure to LIKE and SUBSCRIBE if you enjoyed this guide! Share the video for extra lo...

â–¶ Play video