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 fgbg · Feb 21, 2017 at 02:08 AM · collisiontrigger

Why does OnTriggerEnter trigger infinitely?

I'm following this tutorial. I have an attacking cube running into a rectangle. I scripted it so that when they collide (OnTriggerEvent), one health point will be removed from the rectangle's health pool. Unlike the tutorial, I do not want the attacking cube to destroy itself.

This works fine, except a few milliseconds/frames later all of the rectangle's health points disappear in an instant as well. There is a slight hiccup between the first point vanishing and the rest of the pool (which happens all at once/super fast).

I feel like something strange is happening with OnTriggerEvent. It's not just triggering on the initial collision, or something else is going on that I can't explain. Do I understand this function correctly or am I misusing it?

Health code:

 using UnityEngine;
 using System.Collections;
 
 public class Healthchange : MonoBehaviour {
 
     TextMesh tm;
 
     // Use this for initialization
     void Start () {
         tm = GetComponent<TextMesh>();
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.forward = Camera.main.transform.forward;
     }
 
 
     public int current()
     {
         return tm.text.Length;
     }
 
     public void decrease()
     {
         if (current() > 1)
             tm.text = tm.text.Remove(tm.text.Length - 1);
         else
             Destroy(transform.parent.gameObject);
     }
 }


Monster AI code:

 using UnityEngine;
 using System.Collections;
 
 public class Monster : MonoBehaviour {
 
     bool triggered = false;
     // Use this for initialization
     void Start () {
 
         GameObject tower = GameObject.Find("Tower");
         if (tower)
             GetComponent<NavMeshAgent>().destination = tower.transform.position;
     }
 
     void OnTriggerEnter(Collider co)
     {
             if (co.name == "Tower")
             {
                 co.GetComponentInChildren<Healthchange>().decrease();
                 triggered = true;
              }
     }
 
 
 }

Now, I already fixed the issue at hand by using a boolean to explicitly state if and when the rectangle can be damaged. So after the first hit, it cannot lose health (for now):

 void OnTriggerEnter(Collider co)
 {
     if (triggered == false)
     {
         if (co.name == "Tower")
         {
             co.GetComponentInChildren<Healthchange>().decrease();
             triggered = true;

But I don't want to use a band-aid if I don't have to. I thought Unity was smart enough to do this logic for me. Am I wrong? Why is the collision event triggering more than once when the objects are just standing next to each other?

Comment
Add comment · Show 5
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 oStaiko · Feb 21, 2017 at 06:20 AM 1
Share

How many hitboxes does your monster have? Put a Debug.Log("hit") in the if (co.name == "Tower") bracket, and see if it's calling it like you think it is, It may be something else

avatar image fgbg oStaiko · Feb 23, 2017 at 03:20 AM 0
Share

Thanks for the tip. It seems as though my character is colliding and uncolliding infinitely with the tower while standing next to the tower. I fixed it by ensuring my tower's box collider ALWAYS encompasses my entire player when my player is standing next to it, otherwise the issue persists. Is this normal behavior? This isn't O$$anonymous$$ for my game when I have three towers close to each other.

avatar image oStaiko fgbg · Feb 23, 2017 at 06:13 AM 1
Share

No that's normal. That's what OnTriggerStay is supposed to do. my only other guess would be maybe your enemies are having their hitboxes being moved in and out by code? if its fast you might not see it

Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SBrooks75 · Feb 23, 2017 at 06:16 AM

@fgbg Is your player going to die, play a hurt animation, or bounce of like mario? If so just set what ever character your going to kill or animate to a different layer when it is hit and change your physics 2d settings for that layer not to collide with the enemy or vise versa. Then if you are going to play a hurt animation, run an IEnumerator to set the characters layer back to the original. I had the same issue in my platformer game and this fixed it for me.

Comment
Add comment · Show 1 · 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
avatar image fgbg · Feb 23, 2017 at 05:44 PM 0
Share

The trigger is there to allow my character to start decreasing the target's health. Is there a better way to achieve this efficiently?

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

134 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

Related Questions

Unity 2D sorting item by color 0 Answers

Collision issue, Plz help 1 Answer

How to make my moving object (in game) stop once it collides 1 Answer

[SOLVED] OnTriggerEnter doesn't work? 1 Answer

2D: Destroy object with dynamic collider after exiting object with static collider 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