Gizmos for attack range

I wanted to illustrate the weapon range so it would be easier to verify that the follow target code was
working as intended. So I started with a simple sphere gizmo.

private void OnDrawGizmos()
{
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere(transform.position, weaponRange);
}

image

Then I remembered something cool I saw in a tutorial project a while back (I think it was the 3D game kit) where they had a cone illustration for the weapon range. So I added an adjustable arc instead of the wire sphere.

private void OnDrawGizmos()
{
    Vector3 left = Quaternion.AngleAxis(-weaponAngle / 2f, Vector3.up) * transform.forward;
    Handles.color = new Color(1f, 0f, 0f, 0.1f);
    Handles.DrawSolidArc(transform.position + Vector3.up, Vector3.up, left, weaponAngle, weaponRange);
    Handles.color = Color.red;
    Handles.DrawWireArc(transform.position + Vector3.up, Vector3.up, left, weaponAngle, weaponRange);
}

image

Don’t think I’ll have much use for the adjustable angle for now but it was fun to try. Will just leave it at 360 degrees for the time being.

3 Likes

Well done!
We do mention Gizmos in the course, but we don’t really cover Handles. Good job on working this out!

Works great! Thanks

This is great. I do believe that if you use Handles, it would be a good idea to wrap them in #if UNITY_EDITOR directives because they are in the UnityEditor namespace that is not included in builds. It’s probably fine though, because OnDrawGizmos will not be called in a build. I just like to be careful

1 Like

This is a fine point. Even within the OnDrawGizmos, the code would still need to be wrapped in an #if UNITY_EDITOR block or the build would fail spectacularly. While OnDrawGizmos is never called in a build, all of the Unity Callback methods are still compiled into the final bake.

1 Like

Privacy & Terms