How do I pause/stop an audio clip that I choose, on a certain condition?

Hi guys, i am making a FPS game and its almost complete. The problem i am facing now is implementing the sound effect of the turret i am fighting with.

So it goes like this, the turret stops shooting when i am behing a wall or am out of range. And i also want to stop the shooting sound of it.

I am not using actual bullets for it to shoot me but an angle of 10 degrees(that i am inside of its array line) and per second i am losing 8 health that i stay in this angle. The turret is rotating slowly and i am a bit faster. I’ve coded the turret thats done easily. My script is;

var target : Transform;
var range = 40.0;
var leftFlame : GameObject;
var RightFlame : GameObject;
static var mode = “idle”;
var minigundown : AudioClip;
var minigunup : AudioClip;
var minigunfire : AudioClip;

function Awake()
{
if(!target)
{
target = GameObject.FindWithTag(“Player”).transform;
leftFlame.renderer.enabled = false;
RightFlame.renderer.enabled = false;
}
}

function Update()
{

if(target && CanAttackTarget())
{

//transform.LookAt(target);

var targetRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation,targetRotation,Time.deltaTime * 1.2);

var forward = transform.forward;
var targetDir = target.position - transform.position;
var angle = Vector3.Angle(targetDir, forward);

if(angle < 10.0)
{
DoDamage();
}
else
{
print (“SAFE!”);
}

}

}

var damageTimer = 0.0;

function DoDamage()
{
if(damageTimer == 0.0)
{
damageTimer = Time.time;
}

if((damageTimer+0.05) > Time.time)
{
	return;
}
else
{
Player.HEALTH --;
GameObject.Find("g_health_left").guiText.text = ""+Player.HEALTH;
print(Player.HEALTH);
damageTimer = Time.time;
}

}

function CanAttackTarget()
{
if(Vector3.Distance(transform.position, target.position) > range)
{
Disengage();
return false;
}

var hit : RaycastHit;

if(Physics.Linecast(transform.position, target.position, hit))
{
	if(hit.collider.gameObject.tag != "Player")
	{
		Disengage();
		return false;
	}
	else
	{
		Attack();
		return true;
	}
	
}

	
return true;

}

function Attack()
{
if(mode != “attack”)
{
InvokeRepeating(“FalconAnimate”, 1, 0.05);
mode = “attack”;
AudioSource.PlayClipAtPoint(minigunup, Vector3 (7, 2, 17));
}
}

function Disengage()
{
if(mode != “idle”)
{
CancelInvoke();
mode = “idle”;
leftFlame.renderer.enabled = false;
RightFlame.renderer.enabled = false;
AudioSource.PlayClipAtPoint(minigundown, Vector3 (7, 2, 17));

}

}

function FalconAnimate()
{
if(leftFlame && RightFlame)
{
if(leftFlame.renderer.enabled)
{
leftFlame.renderer.enabled = false;
RightFlame.renderer.enabled = true;
}
else
{
leftFlame.renderer.enabled = true;
RightFlame.renderer.enabled = false;
}
}
else
{
print(“Effects on Turrent not set!”);
}

}

Ok now this might seem confusing but it isnt. Simply what this does is, the turret starts to rotate when there is no object between me and him and when i am not out of range. The turret start shooting after 1 second of this rotation and the animation starts of the turret miniguns. As you can see i have two sound files. Minigun fire is the firing sound with a 1 second delay of minigun winding up at the start, the turning sound before it fires(my minigun has a 1 second delay before starting the shoot animation so it works fine), and the other sound is minigundown that is the minigun winding down sound. Turning without firing. These work really fine. When i get out of range or behing a wall, it stops playing the minigunfire and start playing the minigundown sound since i am going from attack mode to idle mode, that is fine. But the problem is, my sound file is a long one, since this “AudioSource.PlayClipAtPoint(minigunup, Vector3 (7, 2, 17)); } }” function is a PlayOneShot function, i have to keep it 2-3 minutes long to ensure that it doesnt seem buggy even if the play were to stay 3 minutes inside turrets range. But i need to stop or pause the “AudioSource.PlayClipAtPoint(minigunup, Vector3 (7, 2, 17)); } }” function as soon the turret goes idle again so that i would heat the winding down sound without any fire, and also so that the firing sound doesnt continue for 3 minutes when i am behind the wall. I’ve tried everything so far, even attaching an audio source to the turret and trying it with another function. Nothing worked so far, can anybody help me?

Well ok guys, it isnt the perfect solution but i added a second audio file which has a 0.5 second lenght and makes a good looping fire sound and i added this to the 0.5 second delay fixed update function so it works like a loop and only gives a 0.5 second error when i get behind the wall which isnt that unreasonable. I added it to the line;

unction FalconAnimate()
{
if(leftFlame && RightFlame)
{
if(leftFlame.renderer.enabled)
{
leftFlame.renderer.enabled = false; RightFlame.renderer.enabled = true;
}
else
{
leftFlame.renderer.enabled = true; RightFlame.renderer.enabled = false;
AudioSource.PlayClipAtPoint(minigunfire, Vector3 (7, 2, 17));
}
}
else { print(“Effects on Turrent not set!”); }

}

Anyway if someone has the right answer please do tell me…

This is my script that i added to the turret with the sound file minigunfire. It fires when i am visible and stops when i am not. That is great but it is an update function and it fires 200 times per second and the sound is really bad. I want it to play it one time only like a OneShot sound and stop and play it again. Any ideas? Adding a OneShot Play command to an update function. Or a way to carry this outside of the update function?

var target : Transform;

function Update ()
{

	var hit : RaycastHit;

if(Physics.Linecast(transform.position, target.position, hit))
{
	if(hit.collider.gameObject.tag != "Player")
	{
	print ("We are behind the wall now");
	
						audio.Stop();
						return false;
						
		
	}
	else
	{
		print("We are visible to the turret now");
		audio.Play();
		return true;
		
	}
	
}

}