Basically I have been working on a movement script and it has a ‘stamina’ side to it, I want to when my player reaches below 95 stamina that a audio plays (The sound of the player panting) However when the player DOES reach below 95 stamina, I hear the sound get played over and over again really fast and it sounds like a Malfunctioning robot. Here is the area of the code:
This is not my entire code, it is just the area of code that is important
public var myCamera : Camera;
public var lBreath : AudioClip; /* Low breath (Not very tired, just started running */
public var mBreath : AudioClip; /* Medium Breath (Been running awhile, getting a little tired */
public var hBreath : AudioClip; /* High Breath (Basically hes now panting, really tired */
public var Stamina : float = 100;
function Update () {
if(Stamina < 99) {
startBreath();
}
}
function startBreath() {
if(Stamina < 95) {
myCamera.audio.PlayOneShot(lBreath);
}
if(Stamina < 60) {
myCamera.audio.PlayOneShot(mBreath);
}
if(Stamina < 40) {
myCamera.audio.PlayOneShot(hBreath);
}
}
I guess my problem is, How do I make the audio play once and then loop until the player goes above 95 stamina? And then audio changes when he gets below 60, and again when below 40.
public var myCamera : Camera;
public var BreathClips[] : AudioClip; /* Array of audio clips, high, med, low */
public var Stamina : float = 100;
private var OldStaminaLevel : int = 2;
private var NewStaminaLevel : int = 2;
function Start()
{
myCamera.audio.loop = true;
}
function Update () {
if(Stamina < 99)
{
startBreath();
}
else if (myCamera.audio.IsPlaying())
{
myCamera.audio.Stop();
}
}
function startBreath() {
OldStaminaLevel = NewStaminaLevel ;
if(Stamina > 59)
{
NewStaminaLevel = 2;
}
else if (Stamina > 39)
{
NewStaminaLevel = 1;
}
else
{
NewStaminaLevel = 0;
}
if (OldStaminaLevel != NewStaminaLevel)
{
myCamera.audio.PlayOneShot(BreathClips[NewStaminaLevel]);
}
}
Will still be a very choppy transition, but you get the idea.
You can’t use a single audiosource for smooth transitions, you would need multiple audiosources, and fades.
Sorry I just woke up, can you explain the script better? Maybe comment on it, I am confused what you did.
EDIT:
Many errors were created, 3 from the BreathClips variable.
Here:
Assets/Game/Player/Resources/Scripts/PlayerMovement.js(3,23): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Game/Player/Resources/Scripts/PlayerMovement.js(3,26): BCE0043: Unexpected token: :.
Assets/Game/Player/Resources/Scripts/PlayerMovement.js(3,27): UCE0001: ‘;’ expected. Insert a semicolon at the end.
Assets/Game/Player/Resources/Scripts/PlayerMovement.js(285,9): BCE0044: expecting }, found ‘else’.
(That one above is the function startBreath)
Here’s a simpler example:
public var myCamera : Camera;
public var lBreath : AudioClip; /* Low breath (Not very tired, just started running */
public var mBreath : AudioClip; /* Medium Breath (Been running awhile, getting a little tired */
public var hBreath : AudioClip; /* High Breath (Basically hes now panting, really tired */
public var Stamina : float = 100;
function Awake () {
myCamera.audio.loop = true;
}
function Update () {
if (Stamina < 40) {
playBreath (hBreath);
} else if (Stamina < 60) {
playBreath (mBreath);
} else if (Stamina < 95) {
playBreath (lBreath);
} else {
playBreath (null);
}
}
function playBreath (AudioClip breath) {
if (breath == null myCamera.audio.isPlaying) {
myCamera.audio.Stop ();
myCamera.audio.clip = null;
} else if (breath == null) {
return;
}
if (myCamera.audio.clip != breath || !myCamera.audio.isPlaying) {
myCamera.audio.clip = breath;
myCamera.audio.Play ();
}
}
function playBreath (AudioClip breath) {
Error:
BCE0043: Unexpected token: breath.
Sorry for all the trouble guys, I think I am getting how it works now though
function playBreath (AudioClip breath) {
That is creating the error:
Assets/Game/Player/Resources/Scripts/PlayerMovement.js(284,32): BCE0043: Unexpected token: breath.
I am not sure how to fix as the code looks find to me, and I have gone over it several times.
A bit of C# syntax slipped in there.
breath : AudioClip
To explain why your original version wasn’t working: “Update” gets called every frame. You were calling “audio.PlayOneShot(whatever)” over and over every single frame while stamina was low, so it was playing hundreds of times and getting cut off. Daniel’s version only plays the sound if it’s not already playing.