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 /
avatar image
1
Question by Calum-McManus · Jan 08, 2014 at 12:08 PM · c#ontriggerentermeleehitbox

OnTriggerEnter triggering twice on one collider?

I'm Make a Melee Attack system using a hit box to damage everything close by, i'm doing this by having a collider on my player at the front set to "Trigger", then im getting a reference to the objects im attacking by adding anything inside it to a list. The issue is that if i move towards the other object at a certain angle its added to the list twice and im dealing double the damage? Any idea why its doing it? :)

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

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by TonyLi · Jan 08, 2014 at 03:24 PM

It could be a Physics setup issue. It the hit box is a mesh collider, try ticking Convex. Also, depending on how you move/animate your character, maybe you can move in FixedUpdate(). I can set up a scenario, for example, that intermittently "double reports" OnTriggerEnter by giving the player a CharacterController that's moved by CharacterController.Move() in Update().

I also came across this good answer with guidelines on Physics movement and collision detection: http://answers.unity3d.com/questions/7671/guidelines-for-using-rigidbody-collider-characterc.html

[EDIT 1: Also check that your objects don't have two or more colliders.]

[EDIT 2: If you're using rigidbodies, use Rigidbody.MovePosition() instead of changing transform.position directly.]

Comment
Add comment · Show 9 · 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 felixmann · May 13, 2017 at 06:57 PM 0
Share

$$anonymous$$y problem was updating my rigid body's velocity in Update() ins$$anonymous$$d of FixedUpdate. Thanks so much!

avatar image felixmann felixmann · May 13, 2017 at 08:41 PM 0
Share

Sigh, never$$anonymous$$d. $$anonymous$$y issue still persists with FixedUpdate() :(

avatar image TonyLi felixmann · May 14, 2017 at 12:38 AM 0
Share

Do your objects perhaps have two or more colliders? For example, if an NPC has separate colliders for body parts or attachments (e.g., weapons), when the player's trigger collider intersects with the NPC it will receive an OnTriggerEnter message for each collider.

Show more comments
avatar image Calum-McManus · May 14, 2017 at 01:25 AM 0
Share

$$anonymous$$an this is an old question! $$anonymous$$y issue was not realising that scripts like character controllers and nav mesh agents act as colliders as well. So the double up was due to it getting the Collider I had on it as well as the Character controller.

avatar image felixmann Calum-McManus · May 14, 2017 at 04:35 AM 0
Share

Ah I see, I don't think that's my issue :) Thanks for responding to such an old question though!

avatar image felixmann · May 14, 2017 at 07:47 PM 0
Share

TonyLi, you're a legend! Using Rigidbody.$$anonymous$$ovePosition() solved my issue :) Thanks so much!

avatar image TonyLi felixmann · May 14, 2017 at 07:54 PM 1
Share

Awesome! Glad I could help!

avatar image
0

Answer by Tomer-Barkan · Jan 08, 2014 at 12:24 PM

I'm not sure why it would be added twice to the list, maybe something to do with the melee attack mechanics that you use, but anyway the solution is quite simple - use a hashset instead of a list, that will eliminate all duplicates.

To convert a list to a hashset (hence removing all duplicates):

 HashSet<Collider> uniqueCollidrs = new HashSet<Collider>(colliderList);
Comment
Add comment · Show 6 · 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 Calum-McManus · Jan 08, 2014 at 12:36 PM 0
Share

This raises more issues as now this finds all the colliders, so I need a way to differ between them, before I was using "Compare Tag" so that only one of the colliders was being triggered, which worked fine, except for just one side on the other player it would pick up 2. it dosn't make any sense to me

avatar image Tomer-Barkan · Jan 08, 2014 at 12:49 PM 0
Share

if you want one collider per tag, you can use the following linq command:

     List<Collider> uniqueColliders = (from collider in colliders
                                       group collider by collider.tag into tagGroup
                                       select tagGroup.First()).ToList();

Don't forget to add using System.Linq;.

avatar image Calum-McManus · Jan 08, 2014 at 01:00 PM 0
Share

Will that work if all the colliders I want to hit have the same tag?

avatar image Tomer-Barkan · Jan 08, 2014 at 01:26 PM 0
Share

I'm not sure I understand what you want to do anymore... Do you want all the colliders that have some tag, but without duplicates? Or do you just want each tag to appear once in the list (ie you won't have two colliders in the list that have the same tag)?

Why don't you share your current code, it might help shed some light.

avatar image Calum-McManus · Jan 08, 2014 at 01:35 PM 0
Share
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class $$anonymous$$ELEEDamage : $$anonymous$$onoBehaviour {
     [SerializeField]
     private List<GameObject> m_aEnemys = new List<GameObject>();
     // Use this for initialization
     void Start ()
     {
         transform.gameObject.tag = "Enemy";
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
    protected virtual void OnTriggerEnter(Collider enemys)
     {
        if(enemys.gameObject.CompareTag("Enemy"))
        {
            m_aEnemys.Add(enemys.gameObject);
            Debug.Log("Hi");
        }
        
 
        
     }
    protected virtual void OnTriggerExit(Collider enemys)
    {
        m_aEnemys.Remove(enemys.gameObject);
    }
 }

So I want it to list any player in that trigger collider to be listed so that i can then deal damage to all of them in it, but also its critical it can work on 1 tag name, in this case Enemy, as im using the photon System which basically makes multiple clones of the same player so the tag can not be unique between players.

Show more comments
avatar image
0

Answer by yaapelsinko · Apr 28, 2014 at 08:35 AM

I'm facing same problem now.

There is two colliders and I use an additional script component to give them types. So when I want to know what exactly one collider have collided to, I do:

 void OnTriggerEnter(Collider other)
 {
     Debug.Log(other.name)
     switch (other.gameObject.GetComponent<ColliderType>().Type)
     {
         ....
     }
 }

Then in log I see "AttackTrigger" name has shown twice, and second is followed by NullReferenceException. For second time it can't GetComponent() from exactly the same object. This is strange and inconvenient as I have to invent some additional checking for not to fire an event twice.

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
avatar image
0

Answer by Olakehs · Dec 14, 2019 at 04:17 AM

It seems Destroy(other.gameObject) took too long, so I put a other.gameObject.SetActive(false) before and it solved the problem for me

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
avatar image
0

Answer by Sun_Jie · Dec 24, 2020 at 04:56 PM

gameobject[A] has rigibody component,gameobject[B] is a trigger.[A] Initiatively touch [B]. Don't write OntriggerEnter into [B],please put OntriggerEnter script on [A]. It worked for me!

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Weird gravity when enabling gravity OnTriggerEnter 0 Answers

I'm trying to adjust tagged objects meshrenderer in C# code. 1 Answer

OnTriggerEnter doesn't work as it should? (c#) 2 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