Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JumpingCholla · Apr 03, 2014 at 02:11 AM · cameraaudioaudiosourceaudioclipplayclipatpoint

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Infinite_Gamer · Apr 03, 2014 at 02:38 AM 0
Share

I have never done sound but maybe make it a child to the camera?

avatar image getyour411 · Apr 03, 2014 at 02:47 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JumpingCholla · Apr 03, 2014 at 03:50 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges