0
\$\begingroup\$

Is it possible to prevent the collider 2D outline from hiding in Unity?

I am creating some animations in Unity. To create animations I select different parts of a hero and move them frame by frame. Now I am working on the run animation and would like to see the bottom collider during parts movement for the animation creation.

In my case the collider outline is red:

enter image description here

I don't want the red bottom line (which is the collider outline) to disappear while I am creating animations. Should I research the Unity extensions and maybe write my own which would prevent the collider from hiding or is it possible to achieve what I want out of the box somehow?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You can add a script that draws a gizmo along this line. Here's an example:

public class ColliderVisualizer : MonoBehaviour {

    public EdgeCollider2D edge;
    public Color32 color = new Color32(255, 0, 0, 255);

    public void OnDrawGizmos() {

        if (edge == null) return;

        Gizmos.color = color;

        var points = edge.points;

        Vector3 previous = transform.TransformPoint(points[0] + edge.offset);

        for(int i = 1; i < points.Length; i++) {
            Vector3 next = transform.TransformPoint(points[i] + edge.offset);
            Gizmos.DrawLine(previous, next);
            previous = next;
        }

    }
}

Place this on your object, and assign the edge collider to the edge field in the Inspector. Then it will continue drawing the edges in the edge collider whether or not the object is selected.

You can modify this script to work with other types of collider if you need - I was just guessing that yours is an edge collider based on the example image.

\$\endgroup\$
4
  • \$\begingroup\$ Gizmos are one of the most powerful tools we have for making sure invisible things are positioned and working correctly, so it's good to learn your way around them! \$\endgroup\$
    – Kevin
    Mar 19, 2021 at 1:27
  • \$\begingroup\$ I am just curious. Will I have to remove the script manually before building my game? \$\endgroup\$
    – some1 here
    Mar 19, 2021 at 19:46
  • \$\begingroup\$ Not strictly. You could wrap the whole thing inside an #if UNITY_EDITOR...#endif block so that the script basically evaporates out of a built version. \$\endgroup\$
    – DMGregory
    Mar 19, 2021 at 19:48
  • \$\begingroup\$ Cool. Also, I think it is worth noting in your answer that the script should be placed on an object containing the collider. \$\endgroup\$
    – some1 here
    Mar 19, 2021 at 19:53

You must log in to answer this question.

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