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?