- Home /
OnTriggerEnter2D Doesn't work? ;_;
I AM F*****G INFURIATED BECAUSE OF THIS.
I have a gameObject that has a trigger collider2D attached to it and it uses the onTriggerEnter2D function for detecting objects in its vicinity and onTriggerExit2D function for detecting when objects are not in its vicinity. This is an enemy gameObject and it functions with AI.
The problem is, if it detects an object in its vicinity "objectInVicinity" boolean becomes true, but when that objects leaves the vicinity, the "objectInVicinity" boolean is never set to false.
IT HAS A RIGIDBODY2D ATTACHED TO IT, IT IS ACTIVE, IT HAS COLLISION DETECTION ENABLED AS WELL. I DON'T SEEM TO UNDERSTAND WHAT THE PROBLEM IS.
Another object, which is the player game object, implements the same functions, it is pretty much like the above gameObject mentioned but the code works perfectly fine in this one. WHY THIS ARBITRARY BEHAVIOUR UNITY?
Can you post your code? $$anonymous$$aybe there's something that switches layers or anything...
Does the player has the same script attached?
Answer by Bunnybomb7670 · May 22, 2014 at 10:01 AM
Are you naming the function OnTriggerEnter2D ? it HAS to be the correct CASE because otherwise it wont run.
Yes, I have named everything correctly. Not trying to sound cocky but I'm pretty conversant with OnTrigger functions in both 2D and 3D and also collider related functions. Like I said, it works for the enter 2D but it never goes back to false when I call the exit 2D function. Also, why I don't use OnTriggerStay2D, that's because that doesn't work at all.
Answer by PushkarSawant97 · May 22, 2014 at 10:56 AM
var objectInReach : boolean; //This is the boolean I want to change.
function OnTriggerEnter2D(other: Collider2D) {
if(other.gameObject.tag == "Object") {
objectInReach = true;
}
}
function OnTriggerExit2D(other : Collider2D) {
if(other.gameObject.tag == "Object") {
objectInReach = false;
}
}
Answer by pumpkinszwan · May 22, 2014 at 12:32 PM
OnTriggerExit2D doesn't work reliably for me AT ALL.
I have removed it from all my scripts and replaced it with a workaround. In a nutshell I store any items that collide with 'X' in a list during OnTriggerEnter2D & OnTriggerStay2D. I then enact whatever I need to on that list of objects. Then after every frame I clear that list. Repeat. Only items that are colliding in any given frame actually get affected because I'm clearing the list every frame.
Here's an example:
void OnTriggerEnter2D(Collider2D other)
{
objects.Add(other);
}
void FixedUpdate()
{
foreach (Collider2D other in objects)
{
other.attachedRigidbody.AddForce(new Vector2(5,5));
// you could set a flag value here to 'true'
}
objects.Clear();
// or clear any flag indicating the objects are touching - flag will be re-added on the next frame if they are still touching
}
Note that I have only been testing my project on Windows 8.1 and Windows Phone 8, so it's possible that the issue is platform-specific. What platform(s) are you testing on?
Another approach that works for some things is to enact your collision logic in the OnTriggerStay2D, as I find this doesn't trigger even when the OnTriggerExit2D hasn't fired if the two items no longer touch.
Answer by 2dfruity · May 22, 2014 at 12:30 PM
try it with 3D colliders. sometimes the 2D ones don't work for me, but they should do the same thing regardless. have to make them both 3D colliders though.