Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Aug 27, 2020 at 07:15 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can i tag collider? 1 Answer

OntriggerStay - Can this function return boleans ? 0 Answers

can OnTriggerStay trigger more than 2 seconds? 1 Answer

On-click and trigger animation c# 1 Answer

,How do I detect if there are no more enemies of a type in an area? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges