Calculate radius of cone a x distance from origin from angle

Hi,

I am making a shooting mechanic, which checks the angle at which the target transforms are relative to the weapons forward vector. If a target transform is within X degrees of the forward vector it will take damage. This is supposed to resemble a cone-shaped hit detection.

The damage calculation needs to take into account the distance between the point of the forward vector, which is nearest the target transform and a perpendicular point on a X degree rotation of the forward vector. In other words the radius of the point on the forward vector nearest the target and a point on the edge of the “cone”.

I am able to find the nearest point to the target on the forward vector. How do i effectively calculate this distance?

Thanks!

Hello,
Thanks for your drawing, it clarified a lot.
It looks like rather simple trigonometry. Since you have the “forward vector nearest to target”, it looks like you have a right triangle. Knowing this length and the angle lets your know the opposite side of the angle with :

//you'll need to translate in radians since you said x is in degrees
        angleInRadians = Mathf.Deg2Rad(angleInDegrees);
        requestedLength = Mathf.Tan(angleInRadians) * length.magnitude; // if Length is a Vector

Unless I’m mistaken of course.
This is a classic example of “you see that you end up using your trig class in your grown life eventually” :smile:
I’ll just point out that I wonder if your cone applies on the other side of your known length.

1 Like

Ah yes! I somehow managed to graduate a master’s degree in computer science without knowing basic trigonometry. :smiley: Thank you for helping out. The cone is centered around the forward vector so yes it does apply on the other side (if I am understanding your question correctly). The cone is basically defined as such:

Vector3 dir = Target.position - transform.position;

if (Vector3.Angle(dir, transform.forward) < 5)
{
    // within cone.
}
1 Like