Unity中使用Gizmo绘制出攻击范围
Posted Hello Bug.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity中使用Gizmo绘制出攻击范围相关的知识,希望对你有一定的参考价值。
一:圆形
#if UNITY_EDITOR
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
int radius = 5;
int segments = 100;
float deltaAngle = 360f / segments;
Vector3 forward = transform.forward;
Vector3[] vertices = new Vector3[segments];
for (int i = 0; i < vertices.Length; i++)
{
Vector3 pos = Quaternion.Euler(0f, deltaAngle * i, 0f) * forward * radius + transform.position;
vertices[i] = pos;
}
for (int i = 0; i < vertices.Length - 1; i++)
{
Gizmos.DrawLine(vertices[i], vertices[i + 1]);
}
Gizmos.DrawLine(vertices[0], vertices[vertices.Length - 1]);
}
#endif
二:扇形
#if UNITY_EDITOR
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
float angle = 120;
float radius = 5;
int segments = 100;
float deltaAngle = angle / segments;
Vector3 forward = transform.forward;
Vector3[] vertices = new Vector3[segments + 2];
vertices[0] = transform.position;
for (int i = 1; i < vertices.Length; i++)
{
Vector3 pos = Quaternion.Euler(0f, -angle / 2 + deltaAngle * (i - 1), 0f) * forward * radius + transform.position;
vertices[i] = pos;
}
for (int i = 1; i < vertices.Length - 1; i++)
{
Gizmos.DrawLine(vertices[i], vertices[i + 1]);
}
Gizmos.DrawLine(vertices[0], vertices[vertices.Length - 1]);
Gizmos.DrawLine(vertices[0], vertices[1]);
}
#endif
三:矩形
#if UNITY_EDITOR
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
int width = 5;
int length = 10;
Vector3[] vertices = new Vector3[4];
vertices[0] = transform.position + transform.right * width / 2;
vertices[1] = transform.position + transform.right * width / 2 + transform.forward * length;
vertices[2] = transform.position - transform.right * width / 2 + transform.forward * length;
vertices[3] = transform.position - transform.right * width / 2;
for (int i = 0; i < vertices.Length - 1; i++)
{
Gizmos.DrawLine(vertices[i], vertices[i + 1]);
}
Gizmos.DrawLine(vertices[0], vertices[vertices.Length - 1]);
}
#endif
以上是关于Unity中使用Gizmo绘制出攻击范围的主要内容,如果未能解决你的问题,请参考以下文章