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 ;_;");
}
}
}
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…
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
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”.
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?
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!
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.
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
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…
@arkariarn Thank you so much for asking and answering your own question
I spent literally 2+ weeks before finding this info. I hit WALL on my project.
Much appreciated.
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 “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.
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!
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.
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.
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…