- Home /
OnTriggerEnter2D - am I getting it wrong?
I have 2 game objects, lets say them A and B
A has a 2D box collider and isTrigger is selected and this A object is sign static. B has a 2D box collider too and its isTrigger also activated. B also has a kinematic rigidbody2D.
When these two game objects collide with each other OnTriggerEnter2D(Collider2D other) function is only fired on object A not on object B. It is also not fired when two B game objects collides. What am I doing wrong? Any help is appreciated.
Thank you people.
OnTriggerEnter fires when one object has a rigid body and both have colliders. It also only fires on the object that is "intruded". In the case of the 2 object 'B's, at least one if them ha's to be NOT is$$anonymous$$inematic.
Hope this helps
At least one object has a rigidbody.
The message is sent to both the trigger Collider and the Rigidbody (if any) that the trigger Collider belongs to, and to the Rigidbody (or the Collider if there is no Rigidbody) that touches the trigger
The rigidbody does not need to be non-kinematic to detect a trigger
That's not right... can you post a screenshot showing the component setup on each object, and the scripts you're using to detect the OnTriggerEnter? Are you sure you're not collapsing messages in the Log console?
Answer by MergenGames · Sep 09, 2016 at 02:18 PM
Hey Guys, sorry for late response, I really appreciate your help, so first of all thank you.
Below I add some images and code blocks as @tanoshimi requested.
One of my objects which is moving across screen:
The collectible standing still object:
here is my player code block for ontrigger:
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("hoorey");
}
and collectible ontrigger:
void OnTriggerEnter2D(Collider2D other)
{
GameObject head = other.GetComponentInParent<PlayerController>().head;
GameObject tail = other.GetComponentInParent<PlayerController>().tail;
head.transform.localScale = new Vector2(head.transform.localScale.x * 1.5f, head.transform.localScale.y * 1.5f);
head.transform.localPosition = new Vector2(0,0);
tail.transform.localScale = new Vector2(tail.transform.localScale.x * 1.5f, tail.transform.localScale.y * 1.5f);
tail.transform.localPosition = new Vector2(0, tail.transform.localPosition.y + tail.transform.localPosition.y/2);
other.GetComponentInParent<PlayerController>().UpdateHealth(10.0f);
Destroy(gameObject);
}
your second object has no Rigidbody, so no trigger will fire if two collide. Which object also has the PlayerController attached?
PlayerController is Attached to object with rigidbody? There is trigger fired on the object which has only collider. $$anonymous$$y problem is trigger is not fired on the object with rigidbody. And when same rigidbody objects that I mention in my question collides, there is no trigger fired between them.
You said your problem is "It is also not fired when two B game objects collides". That's because neither have a rigidbody attached. (I'm assu$$anonymous$$g your screenshots show ObjectA and then ObjectB?)
If there is no PlayerController attached to tbe other object, then this line will make head null: GameObject head = other.GetComponentInParent().head;
In which case, I'd expect this to generate a null reference exception: head.transform.localPosition = new Vector2(0,0);
Are you sure there's nothing in the Log console?
Answer by m0guz · Sep 10, 2016 at 07:43 AM
I read your posts, other possibility is Layer-based collision detection
First check object's layer in Inspector window, then Go to Edit > Project Settings > Physics2D and check if layer is selected.
Thank you for spending your time on my problem m8, really appreciate it.
In my case, at the end all layers can collide with each other. So I thought it is not necessary. Am I wrong about this idea?