# unity – Generate a 2D field-of-view cone

I have a bunch of randomly generated boids flying around on my screen, but I want a function that can give them a field-of-view where they can “see” other boids or obstacles.

The code I wrote gets all the pixels around the boid in a circle of some size. Then I try to filter through them to find only the ones in front of the boid within a 30 degree cone-shaped field of view.

I am making this project in Unity using compute shaders written in HLSL.

For each boid, I gave a `float2 position` and a `float angle`. In my kernel, I run a function that tries to set the color of all the pixels inside my cone to red. Here’s the code:

``````for(int x = -viewDistance; x <= viewDistance; x++){
for(int y = -viewDistance; y <= viewDistance; y++){
float2 sample = float2(x, y);
float d = length(sample);

if(d <= viewDistance && d > 0){
// sensorAngle is an angle in radians defining the "view margins" of my cone.
float minAngle = boid.angle - sensorAngle;
float maxAngle = boid.angle + sensorAngle;

float theta = acos(sample.x / d);
if(theta >= minAngle && theta <= maxAngle){
float2 p = sample + boid.position;
Result(int2(p.x, p.y)) = float4(1, 0, 0, 1);
}
}
}
}
``````

As you can see I get all the pixels within a certain boundary and filter out ones that are too far away. This works perfectly in retrieving all pixels within a circle around my boid. My problem is with checking if the pixel is within my boid’s view-cone.

I tried following this explanation for that code. This works perfectly, but it seems to “mirror” my cone over the x-axis like this: Also, my boids get generated with a random number between 0 and 360 as their angle. I multiply this angle with `PI / 180` to convert degrees to radians (This all happens on my C# side and then I send a `ComputeBuffer` through to my `Compute Shader`). However, I noticed that when the angle is generated larger that 180 degrees, the cone doesn’t even render at all.

What might be a better method to go about calculating this FOV cone for each of my boids?