- Home /
OnTriggerEnter called only one time?!!
Im trying to do a simple shooting AI but the ai doesn't stop shooting when the Player leaves the collider, he just keeps shooting for ever, anyone could fix it please?
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var shootsound : AudioClip;
var attack : int = 0;
var FireRate = 1;
var canfire = true;
function OnTriggerEnter(pInSight : Collider){
if(pInSight.gameObject.tag == "Player"){
attack = 1;
}
if(!pInSight.gameObject.tag == "Player"){
attack = 0;
}
}
function Update () {
if(attack == 1)
{
Shoot();
}
}
function Shoot () {
if(canfire == true){
var bullet2 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet2.AddForce(transform.forward *BulletSpeed);
GetComponent.<AudioSource>().PlayOneShot(shootsound);
canfire = false;
yield WaitForSeconds(FireRate);
canfire = true;
}
}
I think other people already answered, but just wanted to note that you might think this line does something different than it's actually doing (I guess it shouldn't even compile):
if(!pInSight.gameObject.tag == "Player")
If you want to write "player tag is NOT 'Player'" the proper condition should be:
if(pInSight.gameObject.tag != "Player") //note where the ! is placed
or:
if (!(pInSight.gameObject.tag == "Player")) //note the extra parentheses
Just like "==" means "check if both sides are equal", the "!=" means "check if both sides are different". The not operand "!" before a variable is used with bool variables since it's the only place where "not the value of this variable" means something correctly defined ("not true" means "false", "not false" means "true"... you can't do that with ints, floats, strings, etc, etc).
Answer by Oribow · Oct 16, 2015 at 07:52 PM
OnTriggerEnter
is only called when something enters the trigger. Use OnTriggerExit
instead and always try searching before posting!
Answer by Matheuz · Oct 16, 2015 at 10:33 PM
The OnTriggerEnter is called when the player enters the Trigger. To register when something leaves the Trigger area, use OnTriggerExit instead.
Also, if Shoot is a coroutine, you can't simply call it, you need to call it using StartCoroutine():
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
UnityScript is less strict with coroutines, if the method has a yield in it then it runs as a coroutine, there's no need for the StartCoroutine method.
Answer by omegacraft · Oct 16, 2015 at 10:33 PM
you could use the OnTriggerExit () function like this:
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var shootsound : AudioClip;
var attack : int = 0;
var FireRate = 1;
var canfire = true;
function OnTriggerEnter(pInSight : Collider){
if(pInSight.gameObject.tag == "Player"){
attack = 1;
}
function OnTriggerExit(pInSight : Collider){
attack = 0;
}
}
function Update () {
if(attack == 1)
{
Shoot();
}
}
function Shoot () {
if(canfire == true){
var bullet2 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet2.AddForce(transform.forward *BulletSpeed);
GetComponent.<AudioSource>().PlayOneShot(shootsound);
canfire = false;
yield WaitForSeconds(FireRate);
canfire = true;
}
}
Answer by Badgerfish · Oct 17, 2015 at 02:29 AM
If you want the player to be shot only while in the collider use: OnTriggerStay()
If you want the player to be shot only when entering the collider: OnTriggerEnter() However this will only happen once (specifically when the collider is entered, hence the name)
If you want the player to be shot only when exiting the collider: OnTriggerExit() However this will only happen once (specifically when the collider is exited, hence the name)
Answer by meat5000 · Oct 16, 2015 at 11:58 PM
Its very simple.
if(attack == 1)
{
Shoot();
attack = 0;
}