- Home /
Collision not triggered by box collider
I'm starting out a 2D game. When the rocket collides with the floor nothing happens. Both sprites have box colliders. The rocket has "Is Trigger" checked.
OnTriggerEnter2D is never called in the rocketScript when it collides with the floor.
Please check the Gameobjects and rocketScript below.
-----------------------------rocketScript.cs-----------------------------
using UnityEngine;
using System.Collections;
public class rocketScript : MonoBehaviour {
float camYSize;
// Use this for initialization
void Start () {
camYSize = Camera.main.orthographicSize;
}
// Update is called once per frame
void Update () {
if (transform.position.y < -camYSize) {
GameObject.Destroy(gameObject);
}
}
void OnTriggerEnter2D( Collider2D other )
{
Debug.Log("Trigger");
}
}
ok guys i tested it and i was wrong about OnTriggerEnter2D() can't detect non trigger object . so i will delete the answer so it don't confuses the others.
Answer by tanoshimi · Oct 29, 2014 at 11:16 AM
I don't see anything wrong with the logic in your code and, sure enough, I can make your exact setup work just fine with a couple of simple sprites in a scene. My suggestions of where you might be going wrong are:
The box collider on your rocket is very small (0.0001 wide). If your rocket is moving fast, it is possible that a collision is not being detected as the collider passes through the floor and out the other side in a single frame. Try to change the scale of your objects to make the collider bigger (or slow the rocket down to see if that makes a difference).
Your Update() method is destroying the rocket if it falls below the bottom of the camera view, but I notice your floor object has a negative y value. Is there any chance your script is destroying the rocket before it's had chance to collide with the floor? (try commenting out line 16).
Thank you a lot!! It was the width (0.0001) that caused the collision not to trigger. Would vote you up but my reputation is not enough (yet).
Answer by Zavirjuha · Oct 29, 2014 at 10:06 AM
Both objects should have rigidbodies component to make OnTrigger works.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Plane flyby triggering/help 1 Answer
How do I trigger an event (health loss) only once? 3 Answers
create event on collision 2 Answers
Activate trigger if items colected 1 Answer