- Home /
How to stop turret firing?
Hello everyone. I made a simple turret model. Added guns to it and made it fire. I added a box collider to the turret so whenever the player is in it, the turret will fire. It happened as i thought. Turret firing when player is in the collider. But the problem is i am not being able to make it stop firing when the player is out of the collider. It just keeps firing. Here is the script.
void OnTriggerStay(Collider other)
{
// Track turret
Track();
// Fire turret
if(other.gameObject == player);
{
isFiring = true;
}
Now, how can i make its firing stop as soon as the player leaves the collider???
Answer by Hexer · Aug 05, 2015 at 10:50 AM
OnTriggerExit(Collider other){
if(other.gameObject == player)
{
isFiring = false;
}
Have you tried OnTriggerExit? Also you don't need a semicolon on line 7.
Or you can use an else-statement. which goes as followed.
void OnTriggerStay(Collider other)
{
// Track turret
Track();
// Fire turret
if(other.gameObject == player)
{
isFiring = true;
}
//if other gameObject is not player, isFiring is false.
else
{
isFiring = false;
}
Both your method working.......yyyeeyyy...so thank you too...
ofcourse both are working, because they are the exact same answer ;) (sortof)
Answer by Raddeo · Aug 05, 2015 at 10:53 AM
Use Collider.OnTriggerExit to change isFiring to false when player leaves your collider.
Your answer
![](https://koobas.hobune.stream/wayback/20220611234520im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My turret is a pacifist. 2 Answers
Turret Firing problem [C#] 2 Answers
making an object stop responding with an object at a certain distance away 1 Answer
Turret rotation angle 1 Answer
turning on x and y axis for turret but not using lookat? 1 Answer