- Home /
Detect where line intersects outline shape
I am trying to calculate the width of the limbs on my procedurally generated 2D character.
I need to find the two points circled in red here
I have the character mesh as the mesh you see above, a mesh collider, and also just the outline shape.
The lines originate from inside the mesh so I think a raycast and collision wouldn't quite work.
I would think I need to use the outline shape and loop through all the outline vertices (there are many), until I find the segment of the outline that my white line intersects.
Does anyone have any help they could offer or have a better idea?
Answer by robertbu · Jun 19, 2013 at 01:33 AM
Here are a couple of untested ideas:
Idea 1: Make the white vector v3Dir, and the point in the center of the sphere as v3Pos. To get the width, you follow the vector out a distance you know is beyond the character, and raycast back to the center. You do this for both sides and then you can measure the distance between the two hit points.
var hit : RaycastHit;
var v3CastPoint = v3Pos +
var ray = new Ray(v3CastPoint, -v3Dir,)
collider.Raycast(ray, hit, someLongDisance);
var v3Pt1 = hit.point;
v3CastPoint = v3Pos - v3Dir.normalized * someLongDistance;
ray = new Ray(v3CastPoint, -v3Dir,);
collider.Raycast(ray, hit, someLongDisance);
var v3Pt2 = hit.point;
var width = (v3Pt1 - v3Pt2).magnitude;
'collider' is the collider of the character.
Idea #2: I don't understand how your character mesh is constructed. But if your triangles you are using for the mesh span the width of the character, you can take go above your character and raycast down to the center of the sphere. The RaycastHit will include a reference to the triangle hit. From there you can get the three vertices that makeup that triangle. From these vertices you can get an approximate width.