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?
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”
I’ll just point out that I wonder if your cone applies on the other side of your known length.
Ah yes! I somehow managed to graduate a master’s degree in computer science without knowing basic trigonometry. 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.
}