- Home /
Draw Capsule Gizmo
I want to draw a wireframe capsule gizmo, kinda like this:
void OnDrawGizmosSelected() {
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, 5.0f);
}
Just a capsule. Not a sphere. Is there an easy way to do this?
Answer by Scribe · Apr 02, 2011 at 08:58 PM
Class Variables color Sets the color for the gizmos that will be drawn next. matrix Set the gizmo matrix used to draw all gizmos.
Class Functions DrawRay Draws a ray starting at from to from + direction. DrawLine Draws a line starting at from towards to. DrawWireSphere Draws a wireframe sphere with center and radius. DrawSphere Draws a solid sphere with center and radius. DrawWireCube Draw a wireframe box with center and size. DrawCube Draw a solid box with center and size. DrawIcon Draw an icon at world position in the scene view. DrawGUITexture Draw a texture in screen coordinates. Useful for GUI backgrounds.
these are the only things that gizmos allow for so no ther is no easy way of having a wire frame capsule
Answer by toomasio · Jan 14, 2019 at 03:05 PM
Created a static method if people need.
public static void DrawWireCapsule(Vector3 _pos, Quaternion _rot, float _radius, float _height, Color _color = default(Color))
{
if (_color != default(Color))
Handles.color = _color;
Matrix4x4 angleMatrix = Matrix4x4.TRS(_pos, _rot, Handles.matrix.lossyScale);
using (new Handles.DrawingScope(angleMatrix))
{
var pointOffset = (_height - (_radius * 2)) / 2;
//draw sideways
Handles.DrawWireArc(Vector3.up * pointOffset, Vector3.left, Vector3.back, -180, _radius);
Handles.DrawLine(new Vector3(0, pointOffset, -_radius), new Vector3(0, -pointOffset, -_radius));
Handles.DrawLine(new Vector3(0, pointOffset, _radius), new Vector3(0, -pointOffset, _radius));
Handles.DrawWireArc(Vector3.down * pointOffset, Vector3.left, Vector3.back, 180, _radius);
//draw frontways
Handles.DrawWireArc(Vector3.up * pointOffset, Vector3.back, Vector3.left, 180, _radius);
Handles.DrawLine(new Vector3(-_radius, pointOffset, 0), new Vector3(-_radius, -pointOffset, 0));
Handles.DrawLine(new Vector3(_radius, pointOffset, 0), new Vector3(_radius, -pointOffset, 0));
Handles.DrawWireArc(Vector3.down * pointOffset, Vector3.back, Vector3.left, -180, _radius);
//draw center
Handles.DrawWireDisc(Vector3.up * pointOffset, Vector3.up, _radius);
Handles.DrawWireDisc(Vector3.down * pointOffset, Vector3.up, _radius);
}
}
Answer by Orr10c · Mar 04, 2018 at 10:05 AM
I might be alittle late, ok VERY late but I figured out a way to do this pretty easily and wanted everyone to have a solution to this: I used the Gizmos.DrawMesh function and assigned a capsuled shaped mesh (which unity has as a default 3D shape) to a variable of my script and then sent that variable as a parameter to the DrawMesh function and I could pretty easily mess with the size and radius of the capsule.
works great for me, hope it helps anyone.
a small a code example would help alot. I have no idea how to create a capsule mesh via code.