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 rikki.o · Aug 21, 2014 at 11:45 AM · c#physicssound

How can I play audio while a non player controlled object is moving and stops playing when object stands still?

I am new at scripting and c#.

In my scene there are some physics based objects that slide down slopes, which get steepened via scripts. These objects are boxes that grind down the slope and spheres that roll down the slopes. They make different sounds, so I need a solution that can play a sound (looping) until the object stops moving or is falling off of an edge thus not rolling any more.

I am asking this here as I have no idea on how to start this. I can not use player input to determine when the sound plays.

Thank you for any insight. rikki.o

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 TheCatProblem · Aug 21, 2014 at 01:34 PM

You'll presumably be dealing with an object that has an attached Collider, Rigidbody, and AudioSource. Off the top of my head, here's how I would try to implement what you've described:

 private float thresholdSpeed = 5.5f; // Adjust this value as desired
 
 // OnCollisionStay is called once per frame for every collider that is touching another collider.
 void OnCollisionStay(Collision collisionInfo){
     // Check if the speed of the object exceeds the threshold
     if (rigidbody.velocity.magnitude > thresholdSpeed){
         // If the AudioSource isn't playing, start it
         if (!audio.isPlaying){
             audio.Play(); // Probably best to enable looping of the audio clip in the AudioSource settings or via audio.loop
         }
     }
     else{ // Object is touching something, but isn't moving fast enough
         // If the AudioSource is playing, stop it
         if (audio.isPlaying){
             audio.Pause(); // Pausing is probably better than stopping for looping sounds since it avoids always playing the loop from the start
         }
     }
 }

I haven't tested this, but I think this general idea should work. You'd also need a status variable to indicate when the object isn't touching anything (to stop the sound if the object stops touching any other objects while still travelling at a speed above the threshold), but that could be handled using a boolean set by OnCollisionStay(). Alternatively, using OnCollisionExit() might work (just place audio.Pause() inside).

At any rate, I hope that helps!

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 rikki.o · Aug 23, 2014 at 09:32 AM 0
Share

I am currently getting the game controller running - looking good. And I will be checking your script in a few days. I am commenting here so you don't think I am ignoring your help. Thank you! Thank you! Thank you! rikki.o

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnCollisionStay only detects collisions after a secondary collision 0 Answers

Distribute terrain in zones 3 Answers

Finding the axis of a curving pipe 0 Answers

Performance Question on Kinematic Rigidbodies 1 Answer

Controlling world gravity via object's y axis ; transform.up = -Physics.gravity in C# ? 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