Melee Raycasting by using scanning a cone area

Before any replies, no, I am not using fast-cast or whatsoever open source melee modules out there, unless there’s one that matches with what I am trying to do.

I’m currently trying to create melee raycasts by detecting object hits inside a cone-shaped area. Similar to a melee raycasts on games like CS:GO / Valorant, which melee checks for a small degree of angle offset.

image

The cone is depicted by the green lines:
image

Currently, I have tried using workspace:GetPartBoundsInRadius to get the parts inside the red sphere. Then calculate the degree between collided part and the origin to see whether it is positioned within the cone, if it is, it would be counted as a hit object.

By using workspace:GetPartBoundsInRadius, the part can be detected despite it’s origin is outside of the red sphere. However:

As of now, there is no way to get the exact hit position of the object. If I cast a ray between the center of the hit object and the origin, the angle (yellow line) will exceed the cone area.

This issue is similar to this thread:

Is there any method to achieve such raycasts behavior? Or I would have to cast hundreds of ray per frame inside the cone to detect whether what’s in there?

i would get the angular offset instead, you can calculate what the threshold of the angle should be to get a cone shape without having any rays or getpartsinpart methods

Can you elaborate more on that? How would you get the “angular offset”?

realistically the sphere is a good start, getting objects within it, and to test whether or not its within’ your desired angle to attack, you can use the dot product with the vector from the origin of the attack to the attacked position and the attack direction and see if that is within’ a threshold, if so its within your angle (0-180) and you can attack

Yes, that is exactly what I was doing, however, referring to this image:

The angle between the grey part’s origin and the origin of the sphere exceeds the angle (green line) despite the grey part is within the range.

I have thought of doing size checks but that would not be accounted for different shapes, orientations, meshes etc.

you could get the point of intersection, there are some devforum posts on that, and instead of using the origin

edit, found this post doing the exact thing