Unity3D - 可视化视场锥体无法正确旋转
Unity3D - Visualized field of view cone does not rotate correctly
我写了一个可视化视锥的简单代码。它工作得很好,但是当我将我的对象旋转 90 度时,视锥位于对象的“后面”,而我希望它位于右侧。
这是我获得的可视化视锥的全部代码:
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class FieldOfView : MonoBehaviour
{
private Mesh fieldOfViewMesh;
private Vector3[] vertices;
private Vector2[] uv;
private int[] triangles;
public float fieldOfView = 70.0f;
public float fieldOfViewDistance = 7.0f;
public int rayCount = 32;
void Start()
{
fieldOfViewMesh = new Mesh();
fieldOfViewMesh.name = "fieldOfViewMesh";
GetComponent<MeshFilter>().mesh = fieldOfViewMesh;
vertices = new Vector3[rayCount + 2];
uv = new Vector2[vertices.Length];
triangles = new int[rayCount * 3];
}
void LateUpdate()
{
fieldOfViewMesh.Clear();
vertices[0] = transform.localPosition;
for (int i=0; i<=rayCount; i++)
{
vertices[i + 1] = transform.localPosition
+ VectorFromAngle(transform.eulerAngles.y - fieldOfView / 2.0f + fieldOfView / rayCount * i).normalized * fieldOfViewDistance;
if (i > 0 && i < rayCount)
{
triangles[i * 3 + 0] = 0;
triangles[i * 3 + 1] = i - 1;
triangles[i * 3 + 2] = i;
}
}
fieldOfViewMesh.vertices = vertices;
fieldOfViewMesh.uv = uv;
fieldOfViewMesh.triangles = triangles;
}
Vector3 VectorFromAngle(float inputAngle)
{
return Quaternion.AngleAxis(inputAngle, transform.up) * transform.forward;
}
}
我不确定我做错了什么。是否将度数转换为弧度(或缺少弧度)?
问题已解决。似乎使用 localPosition 是错误的。我将位置与 InverseTransformPoint() 一起使用,现在一切正常。
我写了一个可视化视锥的简单代码。它工作得很好,但是当我将我的对象旋转 90 度时,视锥位于对象的“后面”,而我希望它位于右侧。
这是我获得的可视化视锥的全部代码:
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class FieldOfView : MonoBehaviour
{
private Mesh fieldOfViewMesh;
private Vector3[] vertices;
private Vector2[] uv;
private int[] triangles;
public float fieldOfView = 70.0f;
public float fieldOfViewDistance = 7.0f;
public int rayCount = 32;
void Start()
{
fieldOfViewMesh = new Mesh();
fieldOfViewMesh.name = "fieldOfViewMesh";
GetComponent<MeshFilter>().mesh = fieldOfViewMesh;
vertices = new Vector3[rayCount + 2];
uv = new Vector2[vertices.Length];
triangles = new int[rayCount * 3];
}
void LateUpdate()
{
fieldOfViewMesh.Clear();
vertices[0] = transform.localPosition;
for (int i=0; i<=rayCount; i++)
{
vertices[i + 1] = transform.localPosition
+ VectorFromAngle(transform.eulerAngles.y - fieldOfView / 2.0f + fieldOfView / rayCount * i).normalized * fieldOfViewDistance;
if (i > 0 && i < rayCount)
{
triangles[i * 3 + 0] = 0;
triangles[i * 3 + 1] = i - 1;
triangles[i * 3 + 2] = i;
}
}
fieldOfViewMesh.vertices = vertices;
fieldOfViewMesh.uv = uv;
fieldOfViewMesh.triangles = triangles;
}
Vector3 VectorFromAngle(float inputAngle)
{
return Quaternion.AngleAxis(inputAngle, transform.up) * transform.forward;
}
}
我不确定我做错了什么。是否将度数转换为弧度(或缺少弧度)?
问题已解决。似乎使用 localPosition 是错误的。我将位置与 InverseTransformPoint() 一起使用,现在一切正常。