Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kenji255 · Jul 10, 2017 at 05:38 PM · audiosoundaudiosourcesoundsplayoneshot

AUDIO ISSUES - PlayOneShot... is cutting short... I think?

Okay, so I am making a basic 2D tower defense game, and I'm just wanting my goblin to make gobliny laughing sounds as he approaches the tower, and then make attacky sounds when he gets there, so the goblin has a small box collider for front detection, and he has 2 game objects: one called LaughSounds, and one called AttackSounds. They each have an AudioSource component as well as a script called RandomSoundPlayer that looks like this:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class RandomSoundPlayer : MonoBehaviour {

 private AudioSource audioSource;
 [SerializeField]
 private List<AudioClip> soundClips = new List<AudioClip>();
 [SerializeField]
 private float soundTimerDelay;
 private float soundTimer;
 public float lowPitchRange;        //The lowest a sound effect will be randomly pitched.
 public float highPitchRange;        //The highest a sound effect will be randomly pitched.


 // Use this for initialization
 void Awake () {
     audioSource = GetComponent<AudioSource>();
 }
 
 // Update is called once per frame
 void Update ()
 {
     //Increment timer to count up to restarting.
     soundTimer = soundTimer + Time.deltaTime;

     //If the timer reaches the delay...
     if (soundTimer >= soundTimerDelay) {

         //...reset timer.
         soundTimer = 0f;

         //Choose a random pitch to play back clip at between high and low pitch ranges.
         float randomPitch = Random.Range (lowPitchRange, highPitchRange);

         //Set the pitch of the audio source to the randomly chosen pitch.
         audioSource.pitch = randomPitch;

         // ...choose a random sound.
         AudioClip randomSound = soundClips [Random.Range (0, soundClips.Count)];

         // Play the sound.
         audioSource.PlayOneShot (randomSound);        
     }
 }

}

Then there's the part of the enemy movement script that deals with enabling and disabling the LaughSounds and AttackSounds game objects on the goblin at the appropriate times. I tried to make it as simple as possible for right now, and here's how it looks:

 void OnTriggerStay2D (Collider2D other)
 {
     if (other.gameObject.tag == "Wall") {
         animator.SetBool ("frontClear", false);
         laughSounds.SetActive (false);
         attackSounds.SetActive (true);
     } else {
         if (other.gameObject.tag == "Tower") {
             animator.SetBool ("frontClear", false);
             laughSounds.SetActive (false);
             attackSounds.SetActive (true);
         } else {
             animator.SetBool("frontClear", true);
             attackSounds.SetActive (false);
             laughSounds.SetActive (true);
         }
     }
 }

finally, in the inspector, I dropped all the sounds needed:

alt text alt text

SO, what happens is, when the goblin starts, he starts laughing randomly as he should. Then as soon as he reaches the tower, the LaughSounds object goes inactive as it should and the AttackSounds object becomes active as it should. HOWEVER, at that point, the attack sounds aren't playing properly. I have done some tests, and script is activating, the timer is working properly, but the sounds are making a really short blip for about 1/10 of a second and then cutting off. Sometimes you can't hear anything.

Anyone know why??? D:

screen-shot-2017-07-10-at-112255-am.png (89.8 kB)
screen-shot-2017-07-10-at-112242-am.png (90.8 kB)
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
1
Best Answer

Answer by kenji255 · Jul 10, 2017 at 08:15 PM

Finally figured it out! Thanks to Jinkata for the inspiration! I had to change a lot, but I think it's more efficient. I added an isAttacking public static boolean to the RandomSoundPlayer script and had the enemy movement script switch it when the goblin got to the tower.

 void OnTriggerStay2D (Collider2D other)
 {
     if (other.gameObject.tag == "Wall") {
         animator.SetBool ("frontClear", false);
         RandomSoundPlayer.isAttacking = true;
     } else {
         if (other.gameObject.tag == "Tower") {
             animator.SetBool ("frontClear", false);
             RandomSoundPlayer.isAttacking = true;
         } else {
             animator.SetBool("frontClear", true);
             RandomSoundPlayer.isAttacking = false;
         }
     }
 }

On the goblin, I just have ONE game object called Sounds, with an AudioSource and the RandomSoundPlayer script, which looks like this:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class RandomSoundPlayer : MonoBehaviour {

 private AudioSource audioSource;
 [SerializeField]
 private List<AudioClip> goblinLaughSounds = new List<AudioClip>();
 [SerializeField]
 private List<AudioClip> goblinAttackSounds = new List<AudioClip>();
 [SerializeField]
 private float soundTimerDelay;
 private float soundTimer;
 public float lowPitchRange;                //The lowest a sound effect will be randomly pitched.
 public float highPitchRange;            //The highest a sound effect will be randomly pitched.

 public static List<AudioClip> soundToPlay = new List<AudioClip>();

 public static bool isAttacking = false;


 // Use this for initialization
 void Awake () {
     soundTimer = 0f;
     isAttacking = false;
     audioSource = GetComponent<AudioSource>();
 }
 
 // Update is called once per frame
 void Update ()
 {
     print (isAttacking);
     if (isAttacking == false) {
         //Increment timer to count up to restarting.
         soundTimer = soundTimer + Time.deltaTime;

         //If the timer reaches the delay...
         if (soundTimer >= soundTimerDelay) {

             //...reset timer.
             soundTimer = 0f;

             PlaySound (goblinLaughSounds);
         }
     } else if (isAttacking == true) {
         //Increment timer to count up to restarting.
         soundTimer = soundTimer + Time.deltaTime;

         //If the timer reaches the delay...
         if (soundTimer >= soundTimerDelay) {

             //...reset timer.
             soundTimer = 0f;

             PlaySound (goblinAttackSounds);
         }
     }
 }


 public void PlaySound (List<AudioClip> soundClipList)
 {
     //Choose a random pitch to play back our clip at between our high and low pitch ranges.
     float randomPitch = Random.Range (lowPitchRange, highPitchRange);

     //Set the pitch of the audio source to the randomly chosen pitch.
     audioSource.pitch = randomPitch;

     // ...choose a random sound.
     AudioClip randomSound = soundClipList [Random.Range (0, soundClipList.Count)];

     // Play the sound.
     audioSource.PlayOneShot (randomSound);

     print (randomSound);
 }

}

And for anyone who's new like myself, I dropped the sounds in the inspector, like so:

alt text


screen-shot-2017-07-10-at-21403-pm.png (108.4 kB)
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 Jinkata · Jul 10, 2017 at 10:26 PM 0
Share

Glad I could help in some way :)

avatar image
0

Answer by Jinkata · Jul 10, 2017 at 06:12 PM

How long are your attack sounds? The pitch field will also effect the speed the clip is played at and 3 is pretty high, so what might be happening is your attack sounds are short and your speeding them up by 2.5x - 3x. Try changing the pitches to something like .75 - 1.25 and see what happens.

Comment
Add comment · Show 3 · 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 kenji255 · Jul 10, 2017 at 07:15 PM 0
Share

Well, the attack sounds are pretty long, and I have switched the attack sounds and the laugh sounds, and it works the other way around. It's just which ever game object is inactive and gets set to active suffers this problem... but I did just try turning the pitch to 1 and it was the same... Thanks though!

avatar image Jinkata · Jul 10, 2017 at 07:23 PM 0
Share

Ins$$anonymous$$d of using GameObject.setActive have you tried assigning the laughSounds and attackSounds as RandomSoundPlayers ins$$anonymous$$d of GameObjects and using the enable/disable property?

avatar image kenji255 Jinkata · Jul 10, 2017 at 07:31 PM 0
Share

Hmmmmmmmmm.......... Lemme see what I can do! Thanks for the idea, back in a bit.

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

85 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 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 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 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 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

Audio not coming out of speakers 1 Answer

Question about audio (AudioSource). My ingame sound doesn't sound like the original audio file? 3 Answers

How to start a sound not from it's start ? 2 Answers

Sound Clip isn't playing when triggered. 1 Answer

Play many sounds (60 to 200) without Unity cutting 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