- Home /
Are there any way to show an objects collider outline through scripting?
I was wondering if there were any way to show another objects collider outline through scripting. Are there any way to set an outline active on an object that isn't currently marked? Done in an editor script?
Answer by zmar0519 · Apr 19, 2011 at 04:36 PM
Look up gizmos and look at the draw wireframe box function. That will work in the scene view. In the game view, I recommend using a line renderer with points at each corner, making a collider like box. A third option for the game view would be to purchase vectrocity.
Answer by tomatohorse · Feb 06, 2020 at 07:37 PM
Here's what I used that worked for me:
public class ShowCollider : MonoBehaviour {
[SerializeField] bool showCollider;
void OnDrawGizmos() {
if (showCollider){
Gizmos.color = Color.green;
Gizmos.matrix = this.transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
}
}
Answer by The_Pied_Shadow · Jan 06 at 02:39 PM
If you want this in gameplay
there is a solution if you're looking to visualize a collider in gameplay as I wanted to. I came across this thread, maybe someone else who is trying to do the same thing will too.
Working with 2D colliders, here is my code which uses a line renderer to draw the outline of any 2D collider.
Below "myCollider" is of type Collider2D, and "myLine" is of type LineRenderer with loop enabled.
Mesh mesh = myCollider.CreateMesh(false, false);
mesh.Optimize();
Vector3[] positions = mesh.vertices;
positions = positions.OrderBy(pos => Vector3.SignedAngle(pos.normalized, Vector3.up, Vector3.forward)).ToArray();
myLine.positionCount = positions.Length;
myLine.SetPositions(positions);
Note that you'll need to be using the namespace System.Linq for the OrderBy function. I've tested this successfully with the following 2D colliders: box, circle, capsule, and polygon. Hope it helps someone.