OnTriggerEnter Not working?
Hello, I may feel dumb after your guys' answers because its probably a simple solution. I've been using Unity for years, and OnTriggerEnter and Exit have always been an annoyance. I'm making a zombie game, and the attacking sometimes works, sometimes doesnt. The attacking works by it detecting the player in its "attack zone". If i put a bunch of zombies in the scene, and get close they chase me, and when they're close enough they attack. Some of them attack, others don't. And you probably already know, since i said it works sometimes for other zombies, its pretty much random.. I have rigid bodys on all objects that are going to be colliding, and colliders set as triggers. Here is my code:
using UnityEngine;
using System.Collections;
public class AttackPlayer : MonoBehaviour {
public ZombieAI z;
public bool playerzone;
public float tt = 3;
private float at;
public bool a;
public float ata = 2;
private float aa;
public bool offon;
void Start () {
z = this.GetComponentInParent<ZombieAI>();
at = tt;
aa = ata;
}
void Update () {
//I have the collider turning off and on constantly, and i have this "offon" bool to test it on all the time, or off and on, still get the same problem.
if (offon)
{
at -= Time.deltaTime;
if (!a)
{
if (at <= 0)
{
at = tt;
this.GetComponent<BoxCollider>().enabled = false;
a = true;
}
}
if (a)
{
aa -= Time.deltaTime;
if (aa <= 0)
{
aa = ata;
this.GetComponent<BoxCollider>().enabled = true;
z.once = true;
a = false;
}
}
}
else
{
this.GetComponent<BoxCollider>().enabled = true;
}
if (playerzone)
{
z.agent.Stop();
z.attacking = true;
z.atime -= Time.deltaTime;
//Debug.Log("Attacking");
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
playerzone = true; //never gets enabled
z.agent.Stop(); //Never gets called
z.FollowPlayer(false); // never gets called
Debug.Log("" + other.name + " Entered Zone"); //never debugs
}
if (other.tag == "Door")
{
if (other.GetComponent<OpenCloseDoor>() != null)
{
if (!other.GetComponent<OpenCloseDoor>().open && !other.GetComponent<OpenCloseDoor>().isDead)
{
playerzone = true;
z.agent.Stop();
}
else
{
playerzone = false;
z.attacking = false;
}
}
}
}
public void OnTriggerExit()
{
z.agent.Resume();
z.FollowPlayer(true);
playerzone = false;
}
}
If you read the commented out things, It ill show where the problem is. Thanks if you can help.
Here are images:
This one shows him in front of me, but he never attacks. and the player is clearly in the zone (Player is tagged as "Player").
And as you can see the "playerzone" bool is not being enabled when the player is clearly in the zone.
Answer by Reals7eel · Jun 25, 2016 at 08:00 AM
Yep. I feel really dumb. it was firing the entire time, My "FollowPlayer" function was just disabling it if it was following me (which it was for some reason, it should disable FollowPlayer when the player is in its attack zone) it works fine now
Your answer
![](https://koobas.hobune.stream/wayback/20220612083621im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I get OnTriggerEnter to work? 1 Answer
Move 2 objects as one 0 Answers
Fixedjoints Not Updating Position In UNet 0 Answers
How to delete one Gameobject in a Trigger? 0 Answers
Help with Water Buoyancy Physics (using a trigger) 0 Answers