Unity - Multiple Prefabs with Audio Source - unity3d

I am using a prefab with an audio source attached to it and is used only if you click on the prefab to play a short sound (click sound). There is a scene that I am using this prefab for ~50 times.
There is no problem at all, it works great but I was just wondering is it a bad practice to have so many prefabs each one using its own audio source?
Thank you.

It depends on the use case, but in most cases you can't really avoid it(using more than one audio source). If you look at the inspector of the Audio Source-Component, you see a field for referencing the audio file. So basically even if you have 50 Audio Source-Components it just remains one audio file (in case that you only want to play this single sound). The intention of this approach with multiple audio sources is to get a "physically realistic" feeling. So as in real life if you are out of the range of an audio source you won't hear it.
For example if you have a game with around 50 enemies in the current scene, it's more or less necessary to attach each of them an Audio Source-Component because you want to hear only the enemies, which are in your range.
If you have just one central Audio Source it has to play everything and in most cases you have more work than benefit from it. But a static game like a card game can work very well with this approach, so that you have only one GameObject which holds an Audio Source-Component. If you have more than one sound effect, you have to change the referenced AudioClip programmatically every time you want to play a sound, which is not the currently selected one.
So basically its not really a bad practice, because in most cases it is more or less intended that you have more than one audio source.

Related

unity render a movie as quickly as possible

I am using Unity to create movies. Right now, I create a scene and play it and record it in real time and export the video file. But I'd like render the scene as fast as possible and reduce the time for creating the movie.
How can I render simple scenes faster than real time in Unity? For example, if I have a 10-second movie at 24 frames/second and my machine can render 240 frames in a second, I'd like to finish the job in a second rather than wait for 10 seconds. Working with framerate and time might do the job but I was wondering if there is a better/simpler way to do it.
Unreal Engine's capture movie renders simple scenes faster than real time without me changing anything and I'd like to know if there is something similar in Unity.
Update: You can disable Cap FPS to speed up rendering if you are using the Unity recorder. I am using something else because I want to record the video in a Unity build.

Wwise Resonance Audio Room behaving strangely in Unity

I am trying to implement Resonance Audio via Wwise for a game in Unity, but I have a problem I can not solve for the life of me.
The problem is that the zones where I can hear the room reverb doesn't correspond to the set room boundaries. I've uploaded a video to illustrate the problem. Each room in the game has a WwiseResonanceAudioRoom component attached to it, with no change in scale, that fits the room. But for each of the three rooms, I can only hear the room reverb in some specific part of the room. Some reverb for objects on a longer distance can be heard from parts of the room where the others can't be heard. I've added a debugging box to the game view which outputs the exact data that the ResonanceAudio.cs script is sending to Wwise. In Wwise, all of the reverbed sounds are sent to a "RoomEffectsMasterBus" with the Resonance Audio mixer plugin on it, as well as an ambisonic aux bus (which I turned the volume down on for the video). The regular sound works well, and I'm using Wwise Rooms to separate the room sounds.
How the hell do I make Resonance Audio output room reverbs in the entire room? I haven't touched the Resonance Audio scripts (other than now adding the debug text output). Any help would be greatly appreciated!
I'm using
- Wwise 2019.2.0.7216
- Resonance Audio SDK 2019.2.1.91 from Wwise Launcher
- Unity 2019.2.17f1
- The ResonanceAudioForWWise 1.2.1 Unity-Wwise implementation wrapper scripts for Unity
Here's the video showing the issue:
https://youtu.be/0Y7GXG69IZ0
Room 2 boundaries 1
Room 1 boundaries 2

Unity: Difference between Audio Source, Audio Listener and Audio Clip

I would like to play some sound effects in my Unity project.
I looked for a solution on the official website but I could not get the difference between an Audio Source, an Audio Listener and an Audio Clip.
Is it better to add them programmatically or to use an appropriate component?
Thank you in advance for your patience.
An AudioSource is a component that allows for sound to be played in your scene. It also holds the control options for the audio like Play Pause volume loop and all other properties and methods you need to control how the playback of your sounds work. This component also supports 3D audio, meaning the sound will come form the location of the GameObject it is on, like a persons mouth for example.
Code examples and other properties/methods for AudioSource can be found in the documentation here
the asset type AudioClip is used by the AudioSource for playback. AudioClip contains the file you want to play back as either compressed ogg vorbis, or uncompressed. AudioClip also holds all information about that clip like the length and frequency.
Full documentation on all methods and properties found here
You select what AudioClip the AudioSource plays by setting the AudioSource.clip property.
Finally you have the component AudioListener, that as the name suggest is what listens for the audio in your scene (that is played from an AudioSource). To be able to hear sounds in your scene you need an AudioListener (There is one by default on the main camera, and is limited to one per scene) and be in the range of your AudioSource. Just like AudioSource the AudioListener also has a volume property, but they are not the same. The volume property of the AudioListener dictates the volume for the entire game, where on an AudioSource it only dictates the volume for that specific instance.
the full documentation expliaining all properties and methods can be found here

Playing sound within a certain radius unity

I am looking for some way of making it so that the player can only hear the sounds as they get close to an object. In other words, a sound that only plays within a certain radius, or proximity sound, if this makes any sense.
If anyone knows of a way to do this in unity, any advice would be helpful!
Thanks,
Callum
You can use Unity's built-in 3D audio features to achieve that effect. Simply, if you attach an AudioSource to your desired object (the source of sound), and put the 3d audio sample you'd like to use in it as an AudioClip, you can edit its min/max distance and rolloff values accordingly to set the hearing radius. Note that, in that case you might want to attach your AudioListener to the player object instead, as the distance calculation will be done between the source and the listener.
Alternatively, you could write a small script to cast a sphere to find if the transform of the audio source is inside and change the volume etc. accordingly. Of course, you need to add colliders to your game objects in that case. See: http://docs.unity3d.com/ScriptReference/Physics.SphereCast.html

How to "render" a Box2D scene on iPhone

I'm currently using box2d with cocos2d on iPhone. I have quite a complex scene set up, and I want the end user to be able to record it as video as part of the app. I have implemented a recorder using the AVAssetWriter etc. and have managed to get it recording frames grabbed from OpenGL pixel data.
However, this video recording seems to a) slow down the app a bit, but more importantly b) only record a few frames per second at best.
This led me to the idea of rendering a Box2D scene, manually firing ticks and grabbing an image every tick. However, dt could be an issue here.
Just wondering if anyone has already done this, or if anyone has any better ideas?
A good solution I guess would be to use a screen recorder solution like ScreenFlow or similar...
I think your box2d is a good idea... however, you would want to used a fixed-time step. if you use dt the steps in the physics simulation will be to big, and box2d will be unstable and jittery.
http://gafferongames.com/game-physics/fix-your-timestep/
The frame rate will take a hit, but you'll get every frame. I don't think you'll be able to record every frame and still maintain a steady frame rate - that seems to be asking a lot of the hardware.