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 Griffo · May 03, 2013 at 03:34 PM · audiosourceplayclipatpoint

AudioSource.PlayClipAtPoint

Hi, I have a problem that I hope someone can help me with, I want to play a sound where an RPG hits the ground, so what I have done is made an RPG from the basic game object cylinder, then I dragged it into the prefab folder to make a prefab from it.

I then initiate the prefab when I fire the RPG and it fires fine, I've added a audio source to the RPG with the setting as 3D, Custom RollOff with a distance of 30, also attached to the RPG prefab is a script that initiates an explosion when colliding with anything and I also thought it would play the explosion sound at the same point, this is the bit of code I use to initiate the explosion and sound -

 function OnCollisionEnter(collision : Collision) {
 
     if(!loop){
         loop = true;    // Set loop to true so this destroyRPG() function dont keep repeating
 
         var contact : ContactPoint = collision.contacts[0];
         var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
 
 // Instantiate the explosion and fireball facing away from the impact point
         Instantiate(explosion, transform.position + Vector3(0, -0.15, 0), rot);
            Instantiate(rpgExplosion, transform.position + Vector3(0, 0, 0), rot);
            
         AudioSource.PlayClipAtPoint(explosionSound, collision.contacts[0].point);
 
         destroyRPG();
     }
 }

But no matter where the RPG lands the sound is constant, is this incorrect ?

Comment
Add comment
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

1 Reply

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

Answer by Lockstep · May 03, 2013 at 04:08 PM

The audio source itself does not decide wether it plays a 3d sound or a 2d sound. You have to mark the clip itself as 3d or 3d sound. Select the audio clip in the project panel and check the corresponding check box in the inspector.

Furthermore AudioSource.PlayClipAtPoint creates its own AudioSource and destroys it automatically when the clip is finished. So you don't need to attach a AudioSource component to your RPG.

Comment
Add comment · Show 2 · 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 Griffo · May 03, 2013 at 04:22 PM 0
Share

Thanks for the input, my sound are all set to 3D, but I didn't know AudioSource.PlayClipAtPoint creates its own AudioSource and destroys it automatically when the clip is finished, so that means I need the set it to 3D, Custom RollOff with a distance of 30 at runtime with something like below ?

     private var RPGexplosion: AudioSource;
     
     function Awake(){
         RPGexplosion = AddAudio(fireSoundEnd, false, false, 0.5, AudioRolloff$$anonymous$$ode.Custom, 40);
     }
     
     function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float, RollOff$$anonymous$$ode: AudioRolloff$$anonymous$$ode, $$anonymous$$axDistance: float): AudioSource {
       var newAudio = gameObject.AddComponent(AudioSource);
       newAudio.clip = clip;
       newAudio.loop = loop;
       newAudio.playOnAwake = playAwake;
       newAudio.volume = vol;
       newAudio.rolloff$$anonymous$$ode = RollOff$$anonymous$$ode;
       newAudio.maxDistance = $$anonymous$$axDistance;
       return newAudio;
     }
avatar image Lockstep · May 03, 2013 at 05:08 PM 0
Share

I don't know how exaclty your setup works, but I usually solved this by having the audio source directly on the explosion prefab. You can see every property in the inspecot and change it to your liking, then save the prefab to keep the changes. Since the explosion only needs to play one single clip, it is sufficient to set the clip to your explosion clip, check playonawake and uncheck loop. Then you only need to make sure the explosion lives long enough to play the whole clip.

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

13 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

Related Questions

A better script for a machine gun? 1 Answer

How to route sound from PlayClipAtPoint() to a specific audio mixer group? 2 Answers

Adjust properties of AudioSource created with PlayClipAtPoint 2 Answers

Making AudioSource follow camera? 1 Answer

Why am I able to play an AudioClip without an AudioSource? 1 Answer


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