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
1
Question by Gamershaze · Oct 06, 2013 at 06:58 PM · movementsoundoverlappingrepeating

[Help] How to prevent sound repeating (in this script)

The premise of the below script is; the object it's attached to should move towards the player when their back is turned, and not move when it is being looked at. While it is moving however, it should make a noise which is declared by the movingSound variable.

Since this is all done via the update function, I've created a Boolean for when the sound should/shouldn't be played; to stop the sound from playing every frame. Despite my efforts, it still seems to play over each-other; the sound echoing over itself in-game, and I can not for the life of me figure out why. Any help would be greatly appreciated.

 var target : Transform; //the enemy's target
 var moveSpeed = 3; //move speed
 var stopSpeed = 0;
 var rotationSpeed = 3; //speed of turning
  
 var myTransform : Transform; //current transform data of this enemy
 
 var movingSound : AudioClip;
 public var soundplaying : boolean;
  
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 {
      target = GameObject.FindWithTag("Player").transform; //target the player
      soundplaying = false; //Stop the sound from playing on start.
  
 }
  
 function Update () {
  
     var Speed = moveSpeed;
  
     if (renderer.isVisible)
     {
     myTransform.position += myTransform.forward * stopSpeed * Time.deltaTime;
     rigidbody.isKinematic = true;
     soundplaying = false;
     }
  
     if(!renderer.isVisible)
     {
     rigidbody.isKinematic = false;
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     soundplaying = true;
     
         //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     }
     
     if (soundplaying == true) {
     audio.PlayOneShot(movingSound, 1);
     }
     
     if (soundplaying == false) {
     audio.Stop();
     }
  
 }
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
3
Best Answer

Answer by Owen-Reynolds · Oct 06, 2013 at 08:26 PM

Having a sound play, then not be able to play again for 2 seconds -- that's that same math as being able to shoot a bullet only once every two seconds. Should be able to look up limiting bullets/jumps/... and copy it.

Just in case, the Unity Way of making a delay looks like this. It goes inside all the other "do I want to play the sound" logic:

 float nextSoundTime=0; // can't play sounds if before this time

 // can't play if last one not finished:
 if(Time.time>=nextSoundTime) {
   audio.PlayOneShot(someSound);
   // write down when we will be finished:
   nextSoundTime = Time.time + someSound.length;

}

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 Gamershaze · Oct 06, 2013 at 08:38 PM 0
Share

The problem with this is; let's say the sound clip is ten seconds long. So, that's a ten second delay before the next sound can be played- to prevent the sounds from overlapping. But, there's nothing to say the player won't look at the object before that ten seconds is up. The player looks at it, then looks away, and the object is now moving without the sound playing as the ten seconds from the last one isn't up yet.

This is a good temporary solution though, thanks a ton for trying to help.

avatar image Gamershaze · Oct 06, 2013 at 10:07 PM 0
Share

Accepting this answer as correct.

To fix the issue I posted above, I made sure the sound was meant to be playing AND it was after the previous sound ended via your script. Then I made it so when the person looks at the entity, the sound not only stops, but the nextSoundTime counter is set to zero so the following sound could play when it was needed.

Thanks a ton for your help, and everyone else's.

avatar image
3

Answer by clunk47 · Oct 06, 2013 at 08:45 PM

Just check if the audio is already playing or not.

 if(!audio.isPlaying)
     audio.Play();

Click here for more information on AudioSource.IsPlaying

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 Gamershaze · Oct 06, 2013 at 08:54 PM 0
Share

I'm sorry, I don't quite see how you'd like this function to be implemented to where it'd be helpful with the current issue. If you could explain further, that'd be great. If not, thanks for posting anyways.

avatar image clunk47 · Oct 06, 2013 at 11:08 PM 0
Share

Just implement it into your current code ins$$anonymous$$d of using an extra boolean. Read the documentation. audio.isPlaying is a bool that checks if your sound is playing or not. If it IS playing, this keeps it from playing again. It will only play your audio clip if it is not currently playing... That's the only way to really explain it lol. I don't have the time to completely rewrite your script, I'm just giving you an example and a link to the documentation on how to use it.

avatar image
0

Answer by meat5000 · Oct 06, 2013 at 07:31 PM

How about this?

  if (soundplaying == true)
  {
     audio.PlayOneShot(movingSound, 1);
     soundplaying = false;
  }

Then make the stop routine an else or leave it out?

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 Gamershaze · Oct 06, 2013 at 08:12 PM 0
Share

If I delete the stop routine the sound plays over itself still, regardless of rather or not the object is visible.

When I turn the stop routine into an else statement with that code block you've made, it has the same result as before; the audio continues to play every frame over itself. Thanks for trying to help though.

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

18 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

Related Questions

Sound Problem ! 1 Answer

How to play sound when object stops moving? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Checking if object intersects? 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