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 IsaiahKelly · Nov 15, 2012 at 12:54 PM · audiohit.pointoncontrollercolliderhit

play sound at Impact Point

I want to play an audio source once at the point of impact for the player in 3D space, but also loop it when sliding against something. Here is what I have so far, but it plays on every single contact hit and sounds very bad. I'm using a CharacterController.

 function OnControllerColliderHit(hit : ControllerColliderHit) {
 
     //exit if we hit the ground
     if (hit.moveDirection.y < -0.3) 
     return;
     
     audio.PlayClipAtPoint(impactSound, hit.point);
 }
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

2 Replies

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

Answer by IsaiahKelly · Nov 24, 2012 at 09:26 PM

I solved the problem by creating a audio emitter object in the scene that moves to the player's last hit location and loops.

 function Start() {
     //first find the audio impact object
     soundPoint =  GameObject.Find("AudioHitPoint");
 }
 
 function OnControllerColliderHit(hit : ControllerColliderHit) {
 
     //exit if hitting the ground
     if (hit.moveDirection.y < -0.3) 
     return;
     
     //move the audio impact object to hit location
     soundPoint.transform.position = hit.point;
 
     //then loop the sound when we are hitting something
     if (!soundPoint.audio.isPlaying){
         soundPoint.audio.clip = impactSound;
                soundPoint.audio.Play();
     }
 }
Comment
Add comment · 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
0

Answer by Montraydavis · Nov 15, 2012 at 02:25 PM

I don't see where you check for a specific object colliding, and also, you need an OnCollisionStay.

This may not be what you need 100%, but, is a starter;

 var HitPoint : Vector3 ;
 
 function OnControllerColliderHit(hit : ControllerColliderHit) {
     HitPoint = hit.point;
     //exit if we hit the ground
     if (hit.moveDirection.y < -0.3) 
     return;
 
     if ( hit.collider.tag == "TagYouWantToCheckAgainst" ){
        audio.PlayClipAtPoint(impactSound, hit.point);
     }
 
 }
 
 function OnCollisionStay ( collision : Collision)
 {
     if ( collision.collider.tag == "TagYouWantToCheckAgainst" && !audio.IsPlaying())
     {
         audio.PlayClipAtPoint(impactSound, HitPoint);
     }
 
 }

As far as it "sounding bad", we need a bit more info as to what might be causing this. Does the audio file sound normal when played in iTunes or Windows Media Player, etc?

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 IsaiahKelly · Nov 24, 2012 at 08:52 PM 0
Share

Checking for a specific object isn't a problem and not part of the question. Also audio.IsPlaying doesn't work with audio.PlayClipAtPoint. And OnCollisionStay doesn't work with my CharacterController for some reason. I said “it plays on every single contact hit” and that's why it sounds bad.

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

10 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

Related Questions

How can i choose what channel to play my audio by myself? 1 Answer

Why does sound on Android play at double speed and crackle? 3 Answers

Network Camera, Player 2 Answers

Audio controls disabled??! 0 Answers

Wierd error messages concerning audio play and pause? 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