- Home /
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);
}
}
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;
No problem :) Happens!
I'd appreciate it if you would tick the answer :)
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");
}
}
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;
}
}
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;
Your answer
Follow this Question
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