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 Essential · Mar 06, 2012 at 09:48 PM · soundfunctionstop

How to send a signal when something stops happening?

When my character controller is pushing a crate, the pushing script calls the PlayPushingSound() function on the crate to play a dragging sound… But how can I tell it when I've stopped pushing the crate, thus ceasing the sound playback?

I could just add an update function that's always running on the crate script but seems kinda inefficient. Is there a better way?

(Simplified):

 function OnControllerColliderHit (hit : ControllerColliderHit) {
     var body : Rigidbody = hit.collider.attachedRigidbody;
 
     // Calculate push direction from move direction, we only push objects to the sides
     // never up and down
     var pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);
     
     // push with move speed but never more than half runspeed
     body.velocity = pushDir * pushPower * Mathf.Min (controller.GetSpeed (), controller.movement.runSpeed/2);
     
     hit.gameObject.GetComponent(PushingSoundEffect).PushingSound();
 }


Script attached to crate:

 function PushingSound() {
     // Don't restart audio clip
     if (audio.isPlaying)
         return;
     
     audio.Play();
 }
 @script RequireComponent(AudioSource)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by programmrzinc · Mar 06, 2012 at 10:18 PM

I am pretty sure this would work...

 function OnCollisionExit () {
          audio.Stop() //or whatever it is
         }
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 Essential · Mar 06, 2012 at 11:58 PM 0
Share

I gave it a try as this was a simpler route but no luck — the character never actually goes into the collider in the first place (as he's just pushing it), so I don't believe it could work for this.

avatar image Essential · Mar 07, 2012 at 12:36 AM 0
Share

Oh sorry, I got my triggers and collisions mixed up. I gave it another try but can't seem to get it to recognize the collision with the player, it only recognizes what I assume is the floor.

avatar image
0

Answer by Kiloblargh · Mar 06, 2012 at 11:14 PM

If the crate can slide for a while after you stop pushing it and you want the sound to play until it comes to a rest, just check repeatedly ( but not every frame in Update ) if the rigidbody is sleeping:

 var myRB : Rigidbody // drag on in inspector, don't repeatedly call GetComponent

 function OnCollisionEnter()
 {
 InvokeRepeating("AmIMoving", 0.0, 0.3); //precise enough but much cheaper than Update() 
 }
 
 function AmIMoving()
 {
 if (myRB.IsSleeping)
     {
     audio.Stop();
     Debug.Log("Crate has come to rest.")
     CancelInvoke("AmIMoving");
     }
 }
Comment
Add comment · Show 7 · 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 Essential · Mar 07, 2012 at 12:11 AM 0
Share

Great idea… Though wouldn't this invoke a hundred different copies of AmI$$anonymous$$oving ?

avatar image Kiloblargh · Mar 07, 2012 at 01:38 AM 0
Share

Derrr. Yes, it would in fact do that. Sorry. Easy enough to fix, though: if (!IsInvoking("AmI$$anonymous$$oving")) { InvokeRepeating("AmI$$anonymous$$oving", 0.01, 0.3); }

avatar image Kiloblargh · Mar 07, 2012 at 03:04 PM 0
Share

Another thought- if you already have a repeating test function like that you could use it to check the XZ velocity of the rigidbody and if it's under a certain absolute value lower the volume of your scraping sound, and raise the pitch if it's over another value.

avatar image Essential · Mar 07, 2012 at 04:53 PM 0
Share

Hey the dynamic pitching is a pretty good idea actually.

Just one question, why—at the top of your code—say to drag the rigidbody into the inspector? It's a crate… there's like, a billion crates in the level. I think the only way I can do it is to repeatedly call GetComponent while I'm pushing the crate, right?

avatar image Essential · Mar 07, 2012 at 07:27 PM 1
Share

$$anonymous$$aybe I should totally rewrite my pushing script so there's an invisible trigger collider always in front of the character… Would that be better that my current method of OnControllerColliderHit, do you know?

Show more comments
avatar image
0

Answer by Kiloblargh · Mar 07, 2012 at 09:07 PM

I dunno; I don't entirely trust the CharacterController enough to want to use it. IMHO it feels like something the Unity team cooked up primarily to make the built-in tutorial examples seem as if scripting gameplay in Unity is easier than it is in real life and not something they actually expect you to use in your game. If you can work out your own character control script from scratch you'll have a full understanding of how to make it do what you want it to do.

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

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

6 People are following this question.

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

Related Questions

How can I stop executing a Function in a void Function? 1 Answer

How to Stop all audio 10 Answers

Why does my function play sounds on startup? 4 Answers

How do I pause/stop an audio clip that I choose, on a certain condition? 2 Answers

If a number reaches a certain value send it to another script to play a sound?? 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