- Home /
Unity 2D Trigger Detection is not working.
Hello All,
I am currently trying to get a 2d sprite character moving around on screen and able to interact with the game world. I intend to do this with triggers. The character is a 2d sprite with a sprite renderer and animator setup. It also has a 2d box collider attached and is currently tagged player. I also have a rigidbody assigned to it.
To test out the ability of interacting with the world, I setup a simple 2d box collider with the trigger checkbox marked and called the game object the 'Interaction area'. The idea is that my fully functional character walks into the area and a print statement prints to the console. this is the code that does it (and is attached to the game object i call floor trigger, and also assigned a rigidbody to it):
using UnityEngine;
using System.Collections;
public class Floor_Trigger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter(Collider other){
Debug.Log("Something has entered this zone.");
}
void OnTriggerExit(Collider other){
Debug.Log("Something has entered this zone.");
}
void OnTriggerStay(Collider other){
Debug.Log("Something has entered this zone.");
}
void OnCollisionEnter(Collision collision){
Debug.Log("something has hit me");
}
}
I put in various trigger methods in case any one of them were fired. But so far, absolutely zero luck. And I have looked around on the internet and google (please do not post that I should search google), and so far the methods I have tried out haven't gotten this to work. Is there anything that I am missing?
Just to be sure, in the Start(), add the following debug log:
Debug.Log("Trigger: " + collider.isTrigger);
And make sure it's true.
Edit - removed parenthesis from isTrigger
that's weird... when I try to check it I get this message from the console after I save the script: The member `UnityEngine.Collider.isTrigger' cannot be used as method or delegate
And that is after I also converted the isTrigger portion into an if statement
$$anonymous$$y answer was clearly wrong, this is not a bug just unexpected api. Converted to comment.
I'm either glad or sad to know it's not just me experiencing this problem.
actually hold on a $$anonymous$$ute, i found something interesting:
$$anonymous$$issingComponentException: There is no 'Collider' attached to the "Interaction_Area" game object, but a script is trying to access it. You probably need to add a Collider to the game object "Interaction_Area". Or your script needs to check if the component is attached before using it. Floor_Trigger.Start () (at Assets/GA$$anonymous$$E/SCRIPTS/Triggers/Floor_Trigger.cs:8)
I am guessing that for some reason unity is not treating the 2d colliders the same as the 3d colliders. Im pretty sure that if I replaced what I have with the 3d versions, it would work, but that seems really roundabout. I will look into the 2d colliders more carefully though, see if I can find anything else
btw, as its saying this exception message, I have the 2d box collider as we speak
And just for posterity, you can change the above Debug check for 2D colliders like so: Debug.Log("Trigger: " + collider2D.isTrigger);
Answer by gamenovice · Nov 16, 2013 at 09:57 PM
I found out the issue. The reason it's not working is because I wasn't supposed to use Collider in the first place.
After a few more minutes of investigating, I found an API link to the Collider2D Class: http://docs.unity3d.com/Documentation/ScriptReference/Collider2D.html
From there I found my answer: the OnTriggerEnter2D() method. With this, unity worked exactly the way I expected. Will admit, I had no idea that the team made an entirely separate class for 2D based calculations.
btw, thanks alot tbkn for helping me figure it out, wouldn't have been able to do it without your help. cheers! =)
Yeah, it seems they decided for some reason to split the Collider and Collider2d, so the box collider 2d and box collider do not share the same parent class... I had a similar issue with OverlapSphere
not working with 2d colliders, but I haven't found an alternative 2d method... Which is a shame because I'd like to find all objects within a certain distance of $$anonymous$$e, and am forced to use 3d colliders.
http://answers.unity3d.com/questions/575706/overlapsphere-with-box-collider-2d.html
And another question comes to $$anonymous$$d - since RaycastHit doesn't seem to have a collider2d field, how would Raycast work with 2d colliders?
Speaking of, they also have the Physics2D library, you may be able to find the answer there.
You are a genius my friend :).
Physics2D has an OverlapCircle method. Post your answer in my question about OverlapSphere please so I can accept it.
I guess I should have read the release notes which clearly state these new classes...
Answer by SunriseKingdom · May 01, 2014 at 11:12 AM
Try this
Void OnTriggerEnter2D(Collider2D other)
{
}
Void OnTriggerExit2D(Collider2D other)
{
}
Void OnTriggerStay2D(Collider2D other)
{
}
Hope this help
Answer by TheWarper · Jun 11, 2015 at 10:39 AM
where tf is crohr's comment? I get e-mails about comments on these forums that take me to the top of the forum when I click them. Also, wtf is with this autocorrect on these pages...Dear Unity, your improvements are great with the software...I'm so glad you released 5.1 and fixed soo many things...Please fix these couple issues as well...
Answer by shuudev · Feb 24, 2016 at 01:31 AM
I know what problem u met. Fix : Right-Click in Project(TAB) -> Reimport -> and try again.
Good Luck
Answer by Turzoxpress · Mar 05, 2016 at 04:10 PM
Solved!!!
I faced the same problem but solved it by myself with a simple trick! Follow the steps :
Keep your object\image in moving continuously to use "OnTriggerEnter2D" function perfectly ! Suppose your object\image is remain still in a certain position. now get the X(horizontal) value of your image\object from "Start" function like below :
Now create a function to increase this X(horizontal) value continuously like below :
Now call the fucntion from the "Update () " function :
var temp : float = 0;
function Start () {
temp = Image1.position.x; } function KeepMoving() { Image1.position.x = temp + 0.00001; // increase the x value slightly! Image1.position.x = temp; // again move the fast position } function Update () { keepMoving(); }