GameObject with OnTriggerEnter2D doesn't detect the collision.

I created a new gameobject, and set it to follow my cursor with:

void Update () {
Vector2 mousePos = Input.mousePosition;
transform.position = mousePos;

and then gave it a BoxCollider2D, marked ‘Is Trigger’ and a new script with:

void OnTriggerEnter2D(Collider2D other) {
Debug.Log ("Collision detected ...");

But when it supposedly collides with the other object (a simple cube with a Rigidbody2D and a BoxCollider2D component) nothing happens. I also tried adding the OnTriggerEnter2D into the same script as the first one mentioned, but with the same results.

Since I can’t seem to find any solutions in the ‘Answers’ tread, I’m giving this a go - Do you peeps have any ideas on why it doesn’t collide?

I’m confused on which object has the rigidbody on it. Just to be sure, you need to have a rigidbody on the object set to be the trigger though. Check the “Is Kinematic” check box on the trigger’s rigidbody and you should be good to go.

Input.mousePosition returns pixel co-ordinates relative to the screen, and not a world transform position. You will need to use your Camera’s ScreenToWorldPoint function in order to return a Vector that you can assign as your object’s transform.position. When you assign the mouse position, your object moves off the screen because it is a different value to what you’re expecting.

In terms of collision, make sure that if you’re using trigger collision then your box colliders are marked as ‘Is Trigger’. If you say they collide once in certain circumstances, it may be that there is too much movement per frame and the collision detection is missed. You can try and change the collision detection method to ‘Continuos Dynamic’ for better results.

Found out it was my script for the gameobject to follow the mouse position that was wrong. Should have been:

void Update ()
	Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
	Vector3 pos = r.GetPoint(1);
	transform.position = pos;

Based on Mouse Trail Particle Effect Follow - Unity 3D with C# Programming Tutorial - Simple and Awesome - YouTube