Detecting collision within an arc or pie slice

Hello all,

this is my first post in the forum.

I’m figuring out how to detect collisions within an area similar to a pie or an arc.

I have a character that throws a hook punch and a circular kick. Correct me if I’m wrong but the existing colliders might not me appropriate in this scenario (mesh colliders are not the best solution as I understand).

Casting several rays to form a fan might work.

Any ideas?

Thank you

R3po

I would go for a mesh collider, using a very simple convex mesh. That should not make any serious impact on your application unless you have a huge amout of such objects. Generate a few thousand objects and see what happens in the profiler, but I don’t think you will have a problem.

I would just calculate the angle and check to see if it’s within the desired arc. See this page for a working generic javascript example - http://elenzil.com/esoterica/angle/

On that page, if you ignore the dx and dy components of the output, you’ll notice that theta is the angle you want in degrees.

It’s a fairly simple matter to extrapolate that 2D example to 3D, or you can just ignore the vertical component altogether (I usually do).

It’s been a while since I have looked at it so I can’t remember exactly how they went about it but the 3D Platform Tutorial has Lerpz throwing a punch so it might be worth looking at.

Thanks for the suggestion. Unfortunately, it’s a straight punch, not an arc.

This would be how I would probably do it. Although I may be incorrect in either the order of subtraction for the difference or in whether the front of a Dot Product is positive or negative. However it is either checking a cone in front or behind the player.

Transform player;
Transform toCheck;

float maxDistance;

//The Cos of the maximum angle accepted (-1 is 0 degrees 0 is 90 degrees, -0.5 is 45 degrees, etc...)
float maxAngle;

Vector3 difference = player.position - toCheck.position;

//Check if it's within range of the arc
if(difference.magnitude < maxDistance)
{
//Checks to see if the object is within a 90 degree Cone in front of the player.
  if(Vector3.Dot(player.forward, difference.normalized) > maxAngle)
{
  //This object is within the Cone
}
}

Thanks for the help everyone! With those ideas I can figure out a solution.

If the angle input is in degrees use this:

maxAngle = Mathf.Cos(maxAngleInDegrees * Mathf.Deg2Rad / 2.0f);