What I want to accomplish is to check whether an object is looking in between a minimum and maximum angle in 3D space

for example if a Helicopter is looking to the right...

alt text

it would have to check whether its rotation vector is within a cone like shape alt text

when, the heli's rotation vector is inside, the method returns true, but false otherwise. So in the scenario above, the method would return false.

The answer is fairly simple in 2D as all I have to do is check whether minAngle < heli rot angle < maxAngle or use dot product to check, but in 3D I have to look at 2 dimensions against the Y axis to start.

I could define a cone class and check whether a vector is projected from the origin, but it seemed too excessive as I dont need variables like cone length, start radius, & end radius. I feel like I'm missing something simple that involves a bit of playing around from xz to xz->y. Is it possible to accomplish something similar just by checking heli's rot orientation?

  • \$\begingroup\$ I recommend searching for similar questions. This is a common application of the dot product with the entity's forward vector, so you're very likely to find it's already been answered. \$\endgroup\$
    – DMGregory
    Commented Jan 15, 2018 at 4:58

2 Answers 2


You'll need to get the current direction vector of the object (you can get this with transform.forward in Unity), vectors that point in the directions you care for and the angle between the cone's side and center.

You then simply need to take the dot product of the forward vector and one of the directions, and if it's larger than the cosine of the angle, then the forward vector is in the cone.


It sounds like the Physics.SphereCast() could do the job for you. It cast a sphere in the direction and with the radius that you choose.

Here is a sample script:

public class SphereCast : MonoBehaviour {

public float Distance;
public float distanceToObstacle;
public float sphereRadious;
public string Tag;

void Update()
    RaycastHit hit;

    Vector3 p1 = transform.position;

    if (Physics.SphereCast(p1, sphereRadious, Vector3.down, out hit, Distance))
        distanceToObstacle = hit.distance;

        if (hit.collider.tag == "Enemy")

        Debug.DrawLine(transform.position, hit.point, Color.red, 10);

Here is a GIF with what it looks like: enter image description here

  • 1
    \$\begingroup\$ This solves the problem "How can I detect if there is a collider in a particular direction," which is not the same as the question that was asked, "how can I determine whether an object is facing roughly in a particular direction, within a particular angular tolerance?" \$\endgroup\$
    – DMGregory
    Commented Jan 15, 2018 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .