Created
August 1, 2020 11:23
-
-
Save luisparravicini/50d044a20c67f0615fdd28accd939df4 to your computer and use it in GitHub Desktop.
Method to draw an arc with Unity's Gizmos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class GizmosExtensions | |
{ | |
private GizmosExtensions() { } | |
/// <summary> | |
/// Draws a wire arc. | |
/// </summary> | |
/// <param name="position"></param> | |
/// <param name="dir">The direction from which the anglesRange is taken into account</param> | |
/// <param name="anglesRange">The angle range, in degrees.</param> | |
/// <param name="radius"></param> | |
/// <param name="maxSteps">How many steps to use to draw the arc.</param> | |
public static void DrawWireArc(Vector3 position, Vector3 dir, float anglesRange, float radius, float maxSteps = 20) | |
{ | |
var srcAngles = GetAnglesFromDir(position, dir); | |
var initialPos = position; | |
var posA = initialPos; | |
var stepAngles = anglesRange / maxSteps; | |
var angle = srcAngles - anglesRange / 2; | |
for (var i = 0; i <= maxSteps; i++) | |
{ | |
var rad = Mathf.Deg2Rad * angle; | |
var posB = initialPos; | |
posB += new Vector3(radius * Mathf.Cos(rad), 0, radius * Mathf.Sin(rad)); | |
Gizmos.DrawLine(posA, posB); | |
angle += stepAngles; | |
posA = posB; | |
} | |
Gizmos.DrawLine(posA, initialPos); | |
} | |
static float GetAnglesFromDir(Vector3 position, Vector3 dir) | |
{ | |
var forwardLimitPos = position + dir; | |
var srcAngles = Mathf.Rad2Deg * Mathf.Atan2(forwardLimitPos.z - position.z, forwardLimitPos.x - position.x); | |
return srcAngles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is nice but only works on the xz plane and will ignore Vector.up\down as directions.