- Home /
Making AudioSource follow camera?
For the game I am creating, when I "reload" my gun I want to play a sound clip of a gun reloading. I have done this, the problem is is that when I play the sound the sound does not follow the camera, it plays at the point I activated it. Play() for JS does not work, so I am using PlayClipAtPoint().
Here is my variable for the sound :
var reloadSound : AudioClip;
And here is my code that plays the sound :
function Reload () {
if (currentClip > clipSize)
currentClip = clipSize;
if (Input.GetKeyDown("r") && !reloading && currentClip < clipSize && currentTotalBullets > 0) {
{
reloading = true;
AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1);
}
if (Input.GetButtonDown("Fire1") && currentClip == 0 && currentTotalBullets > 0)
{
reloading = true;
AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1);
}
If you can help, that would be great!
I have never done sound but maybe make it a child to the camera?
In your variable section try
var soundBite : AudioSource;
Assume you have an AudioSource component attached to your player/hierarchy, if not hook that up, then:
soundBite.clip = reloadSound;
soundBite.Play();
If something like that works, eventually you can turn it into a function like
soundToPlay(_clip : AudioClip) {
soundBite.clip = _clip;
soundBite.Play();
}
or some kind of soundmanager. All this is just guess work, I'm working on this too. Also I work with C# so there might be some JS syntax issues.
Answer by sedativechunk · Apr 03, 2014 at 02:54 AM
Hmm... this looks like something simple to fix. 1. Make sure the sound effect's audio file is checked to be a "3D" sound, otherwise it will play at it's default, original format/output. 2. What is this "reload" script attached to? Your playclipatpoint function is playing the sound at "transform.position". If this reload script isn't attached to the camera objecct, then it is NOT using the cameras position but instead whatever the script is attached to. If this script is attached to something besides the game object, then it is using that objects position instead of the camera.
I seen your reply as well, you do not have to make a sound effect a child of the camera. You can load it directly into the camera with a script attached to the camera. I'm not sure what your code is attached to at the moment, but if this script is attached to the camera, then your sound should be playing at the camera's position like I said earlier.
If you wanted sound to play from a different object (which isn't a bad idea), you will have to load the camera as a game object into that object and then load the scripting from it so you can use the transform.position of the camera. I hope that leads you in the right direction. I do alot of sound in Unity so it shouldn't be too difficult.
The script is attached to a child of the main camera. When I get back to my computer I will test your answer. Thanks!
Your answer
Follow this Question
Related Questions
How to control scene lighting based on amplitude of AudioSource? 1 Answer
PlayClipAtPoint Qualify with Type Name 2 Answers
Audio/c#/unity Can i control the duration by time ? 1 Answer
Get audio to just play once 1 Answer
AudioSource won't play 2 Answers