- Home /
FOV visual representation script. Rotation causes offset
I am making a script which will visually represent the FOV of an AI. I have gotten it to work well so far. The result looks like this.
Nice and dandy. Thought it would work. Until i rotated the player. I got massively skewed results and i have sat for an entire day, unable to figure this out and i am currently going bald at a never before seen rate. This is what a 90 degree rotation looks like
And upon rotating the player 180 degrees the raycasts and the actual mesh are going in completely opposite directions. The offset affects the raycast rotation and not the mesh rotation. The script itself:
using UnityEngine;
using System.Collections;
public class FOVRenderer : MonoBehaviour {
//Field of view
public float fov = 30f;
//How many iterations to loop through? Resolution of the mesh
public int iterations = 50;
//Range of the view
public float viewRange = 50f;
//View mesh
MeshFilter meshFilter;
Mesh mesh;
// Use this for initialization
void Start () {
meshFilter = gameObject.GetComponent<MeshFilter>();
mesh = new Mesh();
mesh.name = "FOVMesh";
meshFilter.mesh = mesh;
}
// Update is called once per frame
void Update () {
//Degrees per step
float stepAmount = fov/iterations;
//Store vertices
Vector3[] verts = new Vector3[iterations + 2];
verts[0] = Vector3.zero;
for(int i = 0; i<=iterations;i++)
{
Vector3 direction = DirectionFromAngle(transform.eulerAngles.y-fov/2+stepAmount*i);
Debug.DrawRay(transform.position,direction*viewRange,Color.blue); //This works
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit, viewRange))
{
verts[i + 1] = transform.InverseTransformPoint(hit.point); //This also works
}
else
verts[i + 1] = direction*viewRange; //The assigning of this somehow has a 2*y rotation
}
//Create triangles
int[] tris = new int[(verts.Length - 2)*3];
for(int i = 0; i < verts.Length-2; i++)
{
tris[i * 3] = 0;
tris[i * 3 + 1] = i + 1;
tris[i * 3 + 2] = i + 2;
}
mesh.Clear();
mesh.vertices = verts;
mesh.triangles = tris;
mesh.RecalculateNormals();
}
Vector3 DirectionFromAngle(float angleInDegrees)
{
return new Vector3(Mathf.Sin(angleInDegrees*Mathf.Deg2Rad),0,Mathf.Cos(angleInDegrees*Mathf.Deg2Rad));
}
}
Any help is greatly appreciated. Thanks in advance.
Edit 1: I am stupid. The mesh is the one rotating more than it should. It rotates 2*y. The raycast is being casted correctly.
Answer by $$anonymous$$ · Jun 12, 2016 at 04:05 PM
I solved it! The points that was assigned by default if the raycast hit nothing had an offset of 2*y. Therefore i created a compensation for this offset like this:
verts[i + 1] = (Quaternion.Euler(0, -transform.eulerAngles.y, 0) * direction)*viewRange;
This line would rotate the point of the back 1*y, meaning the overall rotation would be 2*y - 1*y = y leaving us with the correct angle
Hahah yeah. Doesen't help that my maths grades are quite shabby at best.
Your answer
Follow this Question
Related Questions
How to calculate internal angles of a mesh triangle? 3 Answers
Need help with artillery angle of reach prediction 0 Answers
Create a Triangle Mesh. 0 Answers