Question by
hundruxrussell · Aug 28, 2020 at 09:27 PM ·
ontriggerstay
OnTriggerStay() not working
For some reason that I've spent a full hour trying to explain, when I try to check if a civilian enters a zombie trigger, nothing happens. I even set up a debug.log which does absolutely nothing. Is there some massive glaring issue that I don't see?
Civilian Script:
public class CivlianScript : MonoBehaviour
{
public Transform end;
private Vector3 end2;
private NavMeshAgent agent;
private GameObject Civilian;
private float timetime;
public float MaxHealth;
private float Health;
// 1 == Zombie, 2 == Bullet, 3 == Cop Misfire
private int WhatDealtLastDamage;
public GameObject Zombie;
// shit
void Start() {
agent = GetComponent<NavMeshAgent>();
end2 = new Vector3(end.position.x, end.position.y, end.position.z);
Civilian = GetComponent<GameObject>();
agent.SetDestination(end2);
timetime = Time.time + 2;
Health = MaxHealth;
}
void OnTriggerStay(Collider other) {
if (other.gameObject.CompareTag("Zombie")) {
WhatDealtLastDamage = 1;
Debug.Log(other.gameObject.GetComponent<ZombieScript>().Damage);
Health -= other.gameObject.GetComponent<ZombieScript>().Damage;
}
}
// Update is called once per frame
void Update()
{
Debug.Log(Health);
if (timetime < Time.time) {
if (agent.remainingDistance < 10) {
Destroy(gameObject);
}
}
if (Health < 1) {
if(WhatDealtLastDamage == 1) {
Instantiate(Zombie, this.transform.position, this.transform.rotation);
Destroy(gameObject);
}
}
}
}
Zombie Script:
public class ZombieScript : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent zombie;
private Vector3 EnemyPosition;
public LayerMask humanMask;
private GameObject[] enemies;
private List<Transform> enemytransforms = new List<Transform>();
private Transform ClosestEnemy;
private int I;
public float MaxHealth;
private float Health;
public float Damage;
void Awake() {
}
// Start is called before the first frame update
void Start() {
zombie = GetComponent<NavMeshAgent>();
LayerMask humanMask = LayerMask.GetMask("Human");
}
// Update is called once per frame
void Update()
{
I = 0;
enemytransforms.Clear();
enemies = GameObject.FindGameObjectsWithTag("Human");
foreach (GameObject enemy in enemies) {
enemytransforms.Insert(I, enemy.transform);
I++;
}
ClosestEnemy = GetClosestEnemy (enemytransforms, this.transform);
zombie.SetDestination(new Vector3(ClosestEnemy.position.x, ClosestEnemy.position.y, ClosestEnemy.position.z));
}
Transform GetClosestEnemy (List<Transform> enemy, Transform fromThis) {
Transform bestTarget = null;
float closestDistanceSqr = Mathf.Infinity;
Vector3 currentPosition = fromThis.position;
foreach(Transform potentialTarget in enemy) {
if (potentialTarget != null) {
Vector3 directionToTarget = potentialTarget.position - currentPosition;
float dSqrToTarget = directionToTarget.sqrMagnitude;
if (dSqrToTarget < closestDistanceSqr) {
closestDistanceSqr = dSqrToTarget;
bestTarget = potentialTarget;
}
}
}
return bestTarget;
}
void OnTriggerStay(Collider other) {
if (other.gameObject.CompareTag("Bullet")) {
}
}
}
Comment
if triggers doesnt get trigger make sure there is not issues with layers and tags (check the spelling) and make sure there are no issues with the spelling, also make sure atleast one of the objects that collide have rigidibody and both have colliders with one atleast istrigger checked
Your answer
![](https://koobas.hobune.stream/wayback/20220613010600im_/https://answers.unity.com/themes/thub/images/avi.jpg)