In a previous post I talked about creating wires (using Bézier curve) using LineRenderer. I am using a PolygonCollider2D to wrap the wire (see below):

GameObject wire = new GameObject ();
LineRenderer lineRenderer = wire.AddComponent<LineRenderer> ();
PolygonCollider2D wireCollider = wire.AddComponent<PolygonCollider2D> ();

After drawing the curve and attaching the collider (as described in previous post), I added a new component to my gameObject (a WireController):

WireController wireController = wire.AddComponent<WireController> ();

The WireController currently has only one method:

void OnTriggerEnter2D(Collider2D other) {
    Debug.Log ("I'm hit!");

I use a Physics2D.Raycast to check if my mouse pointer is at any point interacting with the wire:

void CastRay() {
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, 100);

    if (hit) {
        Debug.Log (hit.collider.gameObject.name);

If I call CastRay() inside a Update() loop, if (hit) returns true (if the mouse pointer is over the gameObject, in this case, the wire), but the OnTriggerEnter2D method never gets called.

A few observations:

  • collider.isTrigger = true;
  • The WireController is attached to the newly created gameObject (it's visible in the Unity editor at runtime)

Any help is much appreciated!

  • \$\begingroup\$ I might be wrong, but you can't use trigger colliders, you need them to be normal to work with raycasts. (Think about it: there's no enter and exit with a raycast when in a trigger collider) \$\endgroup\$
    – DH.
    Jul 28, 2016 at 18:16
  • \$\begingroup\$ That's what I suspected, so I tried setting isTrigger to false, still did not work :( \$\endgroup\$ Jul 28, 2016 at 18:17
  • 1
    \$\begingroup\$ Ah sorry, I misunderstood you. Please, read the docs more carefully, because the raycast will never trigger the OnTriggerEnter event, because it's when a Collider enters it. A Raycast doesn't have a proper collider, it's different mechanics. \$\endgroup\$
    – DH.
    Jul 28, 2016 at 18:39
  • \$\begingroup\$ I missed that somehow :( Thanks for the help. Then I guess I can call a method whenever I have a hit :) \$\endgroup\$ Jul 28, 2016 at 18:41

1 Answer 1


I created a Helper class responsible for checking if the wire is cut, that contains the following method:

public delegate void CallbackAction();

public static void CheckIfWireIsCut(Camera camera, string tag, CallbackAction callback) {
    if (!Input.GetMouseButton (0)) {

    Ray ray = camera.ScreenPointToRay (Input.mousePosition);
    RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, 100);

    if (hit && hit.collider.gameObject.tag.Equals(tag)) {
        callback ();

In the WireController code, I created a callback method and in the Update() loop I check if the wire is cut:

void Update () {
    GameHelper.CheckIfWireIsCut (Camera.main, gameObject.tag, CutWire);

public void CutWire() {
    Debug.Log ("Cut me!");

This works for my use case. Hope it helps other people with similar issues.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .