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 4shm3x · May 07, 2013 at 09:25 PM · audiodistortion

Distorted Audio using playoneshot

Hey,

I am having an issue with a sound Effect of mine. The background music works just fine but when I go talk call a piece of audio in a script it comes out all distorted. It sounds like there is some echo going on aswell as it sounding like a c skipping playing the same bit over and over. I have checked it in the preview pane and it sounds fine.

Here is my code:

 public class Commands : MonoBehaviour {
 public float Speed = 0.5f;
 public static int Command = 0;
 public float TimeLeft = 0;
 public Texture2D mouth;
 public Texture2D eye;
 public Texture2D nose;
 public Texture2D ear;
 private Texture2D myimage;
 public    AudioClip MouthCommand;
     
 void OnGUI (){
         GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height -120, 200,100), myimage);
     }
     
 void  Start (){
 Command = Random.Range(0,5);
 }
     
     
 void  Update (){
 
 TimeLeft += Time.deltaTime;
 
 if(TimeLeft >2)
 {
 Respawn();
 }
 
 
 transform.Translate(Vector3.up * Time.deltaTime * Speed); // Translate it up
 
     if(transform.position.y >= 1.65f)                    // Respawn it
     {
     Respawn();
     }
     
     switch(Command)
     {
        case 1:
              myimage = mouth;
             audio.PlayOneShot(MouthCommand);
               break;
        case 2:
              myimage = eye;
               break;
               
        case 3:
              myimage = nose;
               break;
        case 4:
              myimage = ear;
               break;
     }
 
 
 
 }
 
 
 
 public void  Respawn (){
     
     TimeLeft = 0;
     transform.position = new Vector3(Random.Range(0.2f,0.9f), 0.4f, transform.position.z);
     Command = Random.Range(1,5);
     
 }
 
 
 
 }
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 whydoidoit · May 07, 2013 at 09:28 PM

Well it keeps playing it while Command = 1 - PlayOneShot doesn't mean that it only plays it once, if you repeatedly call it!

I'd do something like:

 switch(Command)
 {
    case 1:
          myimage = mouth;
           audio.PlayOneShot(MouthCommand);
           Command = 0;
           break;
Comment
Add comment · Show 9 · 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 4shm3x · May 07, 2013 at 09:44 PM 0
Share

I'm such a nub I didn't see that.

Thanks

avatar image whydoidoit · May 07, 2013 at 09:45 PM 0
Share

No problem :) Happens!

I'd appreciate it if you would tick the answer :)

avatar image 4shm3x · May 07, 2013 at 10:37 PM 0
Share

So I have no edited my script so that it works for all the cases so this is it:

  using UnityEngine;
 using System.Collections;
 
 public class Commands : $$anonymous$$onoBehaviour {
 public float Speed = 0.5f;
 public static int Command = 0;
 public float TimeLeft = 0;
 public Texture2D mouth;
 public Texture2D eye;
 public Texture2D nose;
 public Texture2D ear;
 private Texture2D myimage;
 public    AudioClip $$anonymous$$outhCommand;
 public    AudioClip EyeCommand;
 public    AudioClip EarCommand;
 public    AudioClip NoseCommand;
 
     
 void OnGUI (){
         GUI.DrawTexture(new Rect(Screen.width / 2, Screen.height -120, 200,100), myimage);
     }
     
 void  Start (){
 Command = Random.Range(0,5);
 }
     
     
 void  Update (){
 
 TimeLeft += Time.deltaTime;
 
 if(TimeLeft >2)
 {
 Respawn();
 }
 
 
 transform.Translate(Vector3.up * Time.deltaTime * Speed); // Translate it up
 
     if(transform.position.y >= 1.65f)                    // Respawn it
     {
     Respawn();
     }
     
     switch(Command)
     {
        case 1:
              myimage = mouth;
             audio.PlayOneShot($$anonymous$$outhCommand);
             Command = 0;
               break;
        case 2:
              myimage = eye;
             audio.PlayOneShot(EyeCommand);
             Command = 0;
               break;
        case 3:
              myimage = nose;
             audio.PlayOneShot(NoseCommand);
             Command = 0;
               break;
        case 4:
              myimage = ear;
             audio.PlayOneShot(EarCommand);
             Command = 0;
               break;
     }
 
 
 
 }
 
 
 
 public void  Respawn (){
     
     TimeLeft = 0;
     transform.position = new Vector3(Random.Range(0.2f,0.9f), 0.4f, transform.position.z);
     Command = Random.Range(1,5);
     
 }
 
 
 
 }

But now my commands don't work. By commands I mean that when I click on the specified body part The command doesn't work and I don't go to the next one :(

$$anonymous$$ight be worthwhile if you look at my script for each body part also:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$outh : $$anonymous$$onoBehaviour {
 GameObject other;
 public AudioClip $$anonymous$$outhReaction;
     
 void  Start (){
 other = GameObject.FindGameObjectWithTag("Commands");
 }
 
 void  On$$anonymous$$ouseDown (){
  
 
    if(Commands.Command == 1)
    {
     other.GetComponent<Commands>().Respawn();
     audio.PlayOneShot($$anonymous$$outhReaction);
     Score.GameScore += 50;
     }
     else
     print("You Lose");
 }
 }
avatar image whydoidoit · May 08, 2013 at 09:57 AM 0
Share

If you have a LastCommand variable you can do this:

   public static int lastCommand;

   ...

   if(lastCommand != Command)
   {
          lastCommand = Command;


         switch(Command)
             {
                case 1:
                      myimage = mouth;
                      audio.PlayOneShot($$anonymous$$outhCommand);
                      break;
                case 2:
                      myimage = eye;
                      break;
  
                case 3:
                      myimage = nose;
                      break;
                case 4:
                      myimage = ear;
                      break;
             }
   }

 
     
avatar image 4shm3x · May 08, 2013 at 10:01 AM 0
Share

Ok and with this I do not need the command = 0 in each case anymore? Also do I replace this public static int Command = 0; with public static int lastCommand;

Show more comments

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Outer Space Audio Distortion 1 Answer

Audio Distortion effects? 0 Answers

Help with walking script 0 Answers

Audio Destroy Help! 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