Why isn't my OnTriggerEnter2D() function working?
Asked Answered
P

15

0

guys im struggling over an hour on the 2d collision event. Currently I have 2 sprite’s each have a 2d boxcollider and are triggers. I have set the tag of one of the sprite’s to “player” but it still doesn’t work. I have already checked everything ten times and searched the internet for a solution…without success.

please help a fellow unity for the sake of the community xd

btw both sprite’s are animated, but I don’t think that’s the problem…

using UnityEngine;
  
public class collision : MonoBehaviour
{
    void OnTriggerEnter2D (Collider2D other)
    {
        if (other.gameObject.tag == "Player") 
        {
            Debug.Log ("why wont you work ;_;");
        }
    }
}
Pollywog answered 19/3 at 23:55 Comment(0)
P
0

After hours of testing i finally got it to work here is how:

  • First you need one of the two objects to contain a Rigidbody 2D.

  • While i was already doing this it still didn’t work. In my game I’m a bird so I didn’t want gravity to effect my player so I had set the kinematic to true. This caused the object to ignore collision events

so I just set the gravity scale of the object to zero and made the object dynamic (not-kinematic).
This fixed my problems… hope it helps somebody…ever…

Pollywog answered 19/3 at 23:55 Comment(5)

Another potential gotcha that I have encountered with collision triggers not working is if the Physics2D settings don't have interactions turned on for the the layers in question. So, for example, if you have objects in user defined layers such as Player and Missile, but you don't have the check box marked for Player/Missile collisions, then no triggers will be called. You can check your settings in Edit/Project Settings/Physics2D.

Theotokos

Still doesnt work for me, its set for 2D an using js, is trigger has be ticked for the chosen ones, they all have a Rigidbody2D and a circle collider, scripts all '2D's at the end of 'collider' and 'OnTriggeEnter-Exit' What can I do?

Filtrate

Nevermind, is trigger needed to be unticked :)

Filtrate

This is stupid ; I had the same problem (two collider2D, one was as trigger). I fixed it by adding a rigidbody2d component to one of my colliders. I don't even need any rigidbody behavior. Anyone knows why this is necessary ? Can't we just have colliders collide/trigger by themselves without rigidbody involved ?

Myles

kinematic means it is not affected by forces period not just gravity

Mordy
H
0

Thanks for your answer.
For mine the box collider size had defaulted to 1 by 1, when the object itself was 400 x 400, so it was never colliding. Also had to change the offset to match the objects location but works fine now :slight_smile:

Heterogamete answered 2/5, 2016 at 1:9 Comment(0)
N
0

In my case it was the most stupid one I guess (Sorry a newbie in CS, and also was following a poor quality video tutorial !). I had a typo mistake, made it onTriggerEnter2D instead of OnTriggerEnter2D :frowning:

Nae answered 6/6, 2023 at 2:37 Comment(4)

Didn't you get corrected by the program you're coding in?

Dictator

... also check if your parameter is correct. For example, it needs to be a Collider2D for OnTrigger...2D, not a Collider.

Subcommittee

Good this is my case kk !! The code pattern is different and confusing in unity. Becaus has a many different patterns in world ok. But commonly the programers using camelCase. I prefer UpperCamelCase its more legibly and intuitive. But Unity merge the two styles it's very confusing

Introversion

I've just spent the better part of an hour trying to figure this issue out, and your comment was a god-send. Thank you!

Brushoff
S
0

I forget the damn Rigidbody2d. In my case, my player has a Character Controller, so, there is a problema between the Rigidbody2d and the Character controler, so, I made a new gameobject with a rigidbody2d and a box collider and BOM! solution!

thanks for your research! I forgot the behavior of the components in this case! :smiley:

Sustainer answered 6/6, 2023 at 2:28 Comment(0)
M
0

Hi all, mine is a weird one…

I was looking at Brackeys’ How to make a “aa” Replica in Unity, trying to follow the same and encountered the same problem – I set Rigidbody2D to be kinematic and coded my OnTriggerEnter2D method. But when I click “start” button the OnTriggerEnter2D method is not triggered at all.

Then I check his video over and over again, realized that I have to click “pause” first then click “start” – Brackeys said you need to “pause” to give it some time to load everything…

Hope it helps

Markitamarkka answered 2/9, 2017 at 19:46 Comment(0)
K
0

@Pollywog Thank you so much for asking and answering your own question :slight_smile:
I spent literally 2+ weeks before finding this info. I hit WALL on my project.
Much appreciated.

Kreg answered 28/12, 2017 at 2:5 Comment(0)
K
0

Enemy Plane & Script

  • Box Collider2D
  • RigidBody2D (Dynamic, Simulated)
void OnTriggerEnter2D(Collider2D col)
{
    Debug.Log($"Enemy has detected a colission {col.ToString()}");
    if (col.gameObject.tag == "Bullet")
    {
        Destroy(col.gameObject);
        Debug.Log($"Destroyed Bullet {col.ToString()}");
        TakeDamage(1f, 5f); //Subtract Health from Enemy
    }
}

Bullet Prefab

  • Box Collider2D (Is Trigger)
  • RigidBody2D (Dynamic, Simulated, 0 or as low as possible mass

Finally I am getting Debug.Logs :slight_smile: “Collision Detected” and health reporting
Was tricky working with Scripts on Prefabs, make sure you save the prefab as well every time you change its script.

Kreg answered 12/12, 2023 at 9:53 Comment(0)
M
0

You don’t need to make gravity scale 0. Put a rigidbody2d on both of the objects and then you will be able to make them both kinematic with the collision or trigger working. :slight_smile:

Melanson answered 6/6, 2023 at 2:39 Comment(1)

Thank you. Worked for me. However, make sure "simulated" box is ticked.

Templar
B
0

THANKS ! i had the same problem for days ,finally !

  • But in my case just adding a rigidbody2D component set to Kinematic worked fine ! :slight_smile:
    Have a great time guys !
Bracket answered 29/7, 2018 at 18:22 Comment(0)
V
0

Just a heads up, You don’t need it to be dynamic, you can use only one kinematic, and then if its transform isnt changing (aka it is a stationary object) then make sure to set its sleep mode to never sleep!

Voluble answered 4/8, 2018 at 21:34 Comment(0)
D
0

For me it was a stupid mistake but tricky to find.
One of my objects had the “Box Colider 2D” and “Rigibody 2D” set to Kinematic.
The other object was also had the box colider and Is trigger was set, but the problem was that it was “Box Colider” and not “Box Colider 2D”.

Duester answered 20/12, 2023 at 3:43 Comment(0)
V
0

I also had really a lot of problems when configuring the collision between game objects.
After years I wrote every tips in this article https://gamedevelopertips.com/unity-collision-detection-2d/, I’m sure it’ll be helpful if you want to set up properly you collider/triggers properly.

Visage answered 24/5, 2020 at 15:49 Comment(0)
A
0

Believe it or not, in Unity-2021-3-LTS is a different function. Unity will call the first one but not the second one. I found this problem after 2 hours checking.

void OnTriggerEnter2D(Collider2D other)
void OnTriggerEnter2D(Collider other)

If you just want to check the collider, you don’t have to add Rigidbody. just select isTriger on BoxCollilder.

Also, remember you are using the 2D function or the 3D function.
Hope my experiences will help others.

Adim answered 12/12, 2023 at 9:48 Comment(0)
H
0

Mine still does not work even though I have followed all your instructions/ Ideas and the OnTriggerEnter2D still does not work. I do not have any typos, there is already RigidBody2D AND it is not in kinematic mode. Are there any more suggestions to help with my code?,I put In everything Including RigidBody2D( not the kinematic one) And I took all the help from above. Even so my code/detection still does not work. Does anybody know how to help me further?

Herb answered 28/1 at 3:56 Comment(0)
F
0

I spent hours trying to fix this as well. I tried many different things, chatGPT said using a mesh collider should work fine. But nah. It didn’t. When i finally changed the mesh collider to box collider 2d. it worked. So annoying…

Flitch answered 12/12, 2023 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.