void OnSceneGUI() { FOW fow = (FOW)target; Handles.color = Color.white; Vector3 viewAngleL = fow.DirFromAngle(-fow.viewAngle / 2, false); Vector3 viewAngleR = fow.DirFromAngle(fow.viewAngle / 2, false); Handles.DrawWireArc(fow.transform.position, Vector3.up, viewAngleL, fow.viewAngle, fow.viewRadius); Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleL * fow.viewRadius); Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleR * fow.viewRadius); Handles.color = Color.red; foreach (Vector3 r in fow.rays) { Handles.DrawLine(fow.transform.position, fow.transform.position + r); } }
//Creamos un cono de visión, obviamos los objetos que se interponen en el raycast desde la posición del enemigo hacia su frente void MakeMesh() { stepCount = Mathf.RoundToInt(fow.viewAngle * meshRes); float stepAngle = fow.viewAngle / stepCount; List <Vector3> viewVertex = new List <Vector3>(); hit = new RaycastHit2D(); for (int i = 0; i <= stepCount; i++) { float angle = fow.transform.eulerAngles.y - fow.viewAngle / 2 + stepAngle * i; Vector3 dir = fow.DirFromAngle(angle, false); hit = Physics2D.Raycast(fow.transform.position, dir, fow.viewRadius, fow.obstacleMask); //Se añadirá colisión con objeto si el raycast se encuentra algún obstáculo if (hit.collider == null) { viewVertex.Add(transform.position + dir.normalized * fow.viewRadius); } else { viewVertex.Add(transform.position + dir.normalized * hit.distance); } } //Con los vértices obtenidos en el raycast creamos nuestra malla int vertexCount = viewVertex.Count + 1; vertices = new Vector3[vertexCount]; triangles = new int[(vertexCount - 2) * 3]; vertices[0] = Vector3.zero; for (int i = 0; i < vertexCount - 1; i++) { vertices[i + 1] = transform.InverseTransformPoint(viewVertex[i]); if (i < vertexCount - 2) { triangles[i * 3 + 2] = 0; triangles[i * 3 + 1] = i + 1; triangles[i * 3] = i + 2; } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); }
void MakeMesh() { stepCount = Mathf.RoundToInt(fov.viewAngle * meshRes); float stepAngle = fov.viewAngle / stepCount; List <Vector3> viewVertex = new List <Vector3>(); hit = new RaycastHit2D(); for (int i = 0; i < stepCount; i++) { float angle = fov.transform.eulerAngles.y - fov.viewAngle / 2 + stepAngle * i; Vector3 dir = fov.DirFromAngle(angle, false); hit = Physics2D.Raycast(fov.transform.position, dir, fov.viewRadius, fov.ObstacleMask); if (hit.collider == null) { viewVertex.Add(transform.position + dir.normalized * fov.viewRadius); } else { viewVertex.Add(transform.position + dir.normalized * hit.distance); } } int vertexCount = viewVertex.Count + 1; vertex = new Vector3[vertexCount]; triangles = new int[(vertexCount - 2) * 3]; vertex[0] = Vector3.zero; for (int i = 0; i < vertexCount - 1; i++) { vertex[i + 1] = transform.InverseTransformPoint(viewVertex[i]); if (i < vertexCount - 2) { triangles[i * 3 + 2] = 0; triangles[i * 3 + 1] = i + 1; triangles[i * 3] = i + 2; } } mesh.Clear(); mesh.vertices = vertex; mesh.triangles = triangles; mesh.RecalculateNormals(); }