- Home /
Boxcollider2D, Trigger not working
Hello,
as first i know there are many questions like mine but I already tried what i found.
So, I'm using Unity 5.3.5f1 and im trying to get trigger enter on 2D box.
I'm "shooting" 2D circle with circle collider and non kinematic rigidbody to static box collider (everything 2d) . If i dont set "is trigger" they bounce but dont react to "oncollisionenter2d" if they are (those boxes) "is trigger" enabled they dont react to "ontriggerenter2d"
Any help? I'm desperate :D Thanks in advance :)
Try making the circle has the "is kinematic" enabled and the rectangle has it disabled. In the older version of Unity that I have used, sometimes two object those have "is kinematic" both enabled or disabled don't react to each other. Plus, check if your script is written as you desired.
@$$anonymous$$imi from docs i found this "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."
function OnTriggerEnter2D(other: Collider2D){
Debug.Log("Trigger: "+other);
}
function OnCollisionEnter2D(coll: Collision2D){
Debug.Log("Collision: "+coll);
}
+code of my trigger/collision (and inspector u can see in post) any help?
Answer by brunocoimbra · Jun 06, 2016 at 06:05 PM
You need to attach your script to your trigger...
By your inspector, you did NOT attached the script!
If you did, please update your post with your ACTUAL script and inspector! We can't help if you don'y give us any REAL info.
Okay, i found problem. I expected my code will accept any collision from any object attached to any other object. So i have to run script to every trigger enabled object to get message. Thank you.
Is there any possibility to do that without attaching code (new one) to every platform and do it somehow globally?
Answer by Dibbie · Jun 05, 2016 at 04:27 PM
Cases matter in variable names - so set the "Is Trigger" boolean/checkbox in the inspector ON for your bullet.
In your code, im assuming thats attached to your bullet, it may look something like this (untested code - C#):
OnTriggerEnter2D (Collider2D col){
//destroy your target you collided with
Destroy(col.gameObject);
//destroy yourself
Destroy(this.gameObject);
}
////or a more specific check
//OnTriggerEnter2D (Collider2D col){
//if(col.gameObject.tag == "Enemy"){
////destroy your target
//Destory(col.gameObject);
////destroy yourself
//Destroy(this.gameObject);
//}
In the second example (that is commented out), the "Enemy" tag is something youd have to create in the inspector, clicking on any object and going to the inspector, near the top left where it says "Tag" and normally it will be some default tag, click on the drop-down, and create a new tag. Make sure the name, spelling, and capitalization of the tag you create in the inspector, reflect the same in your code.
Your example script is correct. And by the way thanks for adding the "tag" issue, I forgot that :P
haha i accidentaly posted 2 same pictures ill change it tommorow now im on ntb.. of course i have is trigger enabled on box.. i will try kimi's "is$$anonymous$$inematic" but im not sure cuz i think i already did this ...the problem is .. i HAVE istrigger enabled, i tried is$$anonymous$$inematic on/off (but only projectile has rigidbody, not both) and i did
function ontriggerenter(collcollider2d){
debug.log("blabla");
} //now its not 100% correct by syntax
and it does really nothing.. it just dont trigger :/ i had that ontriggerenter function at my previous game with older version which worked like a charm :/ now i did same thing and nothing here :(
@dibbie i dont want destroy i have my own code to do something else so answer is out of topic bit.. its nothing with tags and destroying etc, its about unity is not getting triggerEnter event
Did you read @dibbie's answer? Your code is complete nonsense:
function ontriggerenter(collcollider2d){
debug.log("blabla");
}
OnTriggerEnter, not ontriggerenter.
But, seeing as you appear to be trying to use 2D physics, this should actually be OnTriggerEnter2D.
collcollider2d? probably coll : Collider2D
Debug.Log, not debug.log
https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers
$$anonymous$$ister! Did u read note? Code is not correct by syntax..
function OnTriggerEnter2D(other: Collider2D){
Debug.Log("Trigger: "+other);
}
function OnCollisionEnter2D(coll: Collision2D){
Debug.Log("Collision: "+coll);
}
this is my real code
please! i told i know how to do triggers, i already did game using them, i used same way and something is wrong.. im sorry for my reaction but im irritated