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 tfodor · May 10, 2015 at 06:47 PM · fmodplayoneshotrepeating

Sound Repeating every frame..Want it only once!

Hey guys. I'm trying to get a sound clip to play when my cursor falls over a gameobject, but it's playing every frame. I've tried some of the suggestions around here but am struggling to bring them into my code. Please help! I've attached my code here:

using UnityEngine; using System.Collections;

public class Targeting : MonoBehaviour { public GameObject scope; // The main score that is set infront of the ship to allow aiming of ship private int Range= 70; // The range of the array private FMOD.Studio.EventInstance lazer; //variable for lazer event

 // you're trying to assign the audio event to a variable so that the reddit thing makes sense    
 //when you leave cursor..set a variable to true that resets the if statement 

 void  Update ()
 {
     RayShoot();//Stast the rayShoot array
 }


 void  RayShoot ()
 {
     RaycastHit hit;
     
     Vector3 facingDirection= transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(transform.position, facingDirection * Range, Color.blue);
     scope.GetComponent<Renderer>().material.color = Color.blue;
     lazer = FMOD_StudioSystem.instance.GetEvent ("lazer_gun");

     
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Enemy") //Checks the tag
     {
         scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red

     
        }
     
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Alliance") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.green; //Sets the scope color green

     }
     
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "Meteor") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red

         FMOD_StudioSystem.instance.PlayOneShot("event:/lazer_gun", gameObject.transform.position);


     }

     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AllianceMothership") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.green; //Sets the scope color green
     }
     
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemyMothership") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.red;  //Sets the scope color red
     }
     
    if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemySatellite") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.red;  //Sets the scope color red

     }
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AllianceSatellite") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.green;  //Sets the scope color green
     }
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "AlliancePlanet") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.green;//Sets the scope color green
     }
     if (Physics.Raycast(transform.position, facingDirection,out hit, Range) && hit.transform.gameObject.tag == "EnemyPlanet") //Checks the tag
        {
         scope.GetComponent<Renderer>().material.color = Color.red; //Sets the scope color red
     }

 }



}

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

Answer by Cherno · May 10, 2015 at 08:17 PM

 AudioClip myClip;
 bool isPlayingSound = false;
 
 //to start the CoRoutine:
 StartCoRoutine(PlaySound(myClip));
 
 IEnumerator PlaySound(AudioClip clip) {
      if(isPlayingSound == true) {
           break;
      }
      isPlayingSound = true;
      GetComponent<AudioSource>().clip = clip;
      GetComponent<AudioSource>().Play();
      yield return new WaitForSeconds(clip.length);
      isPlayingSound = false;
      yield return null;
 }
 
 
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 tfodor · May 10, 2015 at 08:22 PM 0
Share

Thanks for the reply! I'm having some difficulty translating this into F$$anonymous$$OD. Could you perhaps explain it a bit or just blend it with what I have? Sorry for the difficulties and thank you again.

avatar image Cherno · May 10, 2015 at 08:27 PM 0
Share

I can't as I have no experience with F$$anonymous$$OD, but in the end you would just have to change the ...Play line so F$$anonymous$$OD plays a clip, and somehow access the clip's length so the Coroutine knows how long it should wait. The whole idea is to use bool to keep track of wether a clip is playing. A clip is only played anew if the bool is false. The CoRoutine that plays the clip will check if the bool is false, play the clip, and then wait for the clip to finish before setting the bool to false again.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

PlayOneShot error: The specified channel has been reused to play another sound 0 Answers

Adaptive audio / channel switching 0 Answers

What are the possible causes if there's no sound at all, using FMOD? 0 Answers

[SOLVED] Cannot create FMOD: Sound instance for resource End of file unexpectedly reached while trying to read essential data,Cannot Create FMOD Sound Instance For Resource 1 Answer

Playing AudioClip over Network 0 Answers


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