- Home /
Why doesn't OnTriggerEnter2D get called?
I just started using the 2D tools in Unity but this behavior seems a little odd: I have two sprites with (for testing purposes) each a Box Collider 2D on them, both set to isTrigger.
One has a script attached to control it and register collisions, but the method never gets called even though they should collide! Also if this for some (obvious) reason can't work, what would be a work around without using Rigidbody2D?
PlayerController.cs:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
void Start () {
}
public void Update()
{
float horizontal = Input.GetAxis("Horizontal") * 0.1f;
float vertical = Input.GetAxis("Vertical") * 0.1f;
transform.Translate (horizontal, vertical, 0);
}
void OnTriggerEnter2D(Collider2D other) {
Debug.Log ("was hit");
}
}
EDIT
Noticed the thing about triggers and reverted to normal BoxColliders. Both Objects are on the same z-value. To clear things up:
PlayerObject:
SpriteRenderer
PlayerControllerScript (see below)
BoxCollider2D
OtherObject:
SpriteRenderer
BoxCollider2D
void OnCollisionEnter2D(Collision2D coll) { Debug.Log ("was hit"); }
Answer by maxkunes · Jan 03, 2014 at 07:31 PM
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "Tag") {
}
}
That is my function to do triggers/collision and it works perfectly
Note!! Collision 2d will only collide with a object with a 2d collider
Sorry, but that is as unhelpful as anything. I clearly wrote BOTH objects have a BoxCollider! I am out of the blue assu$$anonymous$$g that you are indeed using a Rigidbody2D on your object which is what I didn't want to do.
How is that unhelpful and by the way i am not using rigid body you have a unproper way of registering collisions and triggers.
If you did not know a collision is the same as a trigger in most conditions and also if you are using what im using the object does not need a rigidbody because in my current game right now I am using your code edited a bit of course and it is working fine with my fix maybe your missing something but I am not sure why this is not working for you but it is for me..
And also just curious even if this is just a test unless in the future you are going to use raycast for collisions which is a pain why would your character be a trigger because making something a trigger disables the collider itself so you would need another way of colliding with objects unless you would use a trigger collider and a seperate polygon collider.
How are you going to achieve this it will help me in future projects because i hate using raycast and triggers?
It is unhelpful because it in no way relates to my question. Yeah I noticed the stuff about the isTrigger things and changed it.
Your answer
Follow this Question
Related Questions
Unity2D: Renderer off/ on by triggers 0 Answers
Wanting to have a sprite animation to only start playing once the player collides with it? 1 Answer
why isn't my OnTriggerEnter2D() function working? 14 Answers
OnTriggerStay2D Breaks When Adding AnimationController 1 Answer
How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer