Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Reals7eel · Jun 24, 2016 at 08:27 PM · unity 5rigidbodytriggercodepageenter

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: alt text

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"). alt text

And as you can see the "playerzone" bool is not being enabled when the player is clearly in the zone.

fd.png (209.8 kB)
fds.png (9.4 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 alt text

alt text


fgdgfd.png (189.0 kB)
fgggg.png (8.5 kB)
Comment
Add comment · Share
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

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

102 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

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


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