So I have a simple script, it casts a ray from a point and upwards. I have a Debug.DrawRay that draws a ray from the start point and up, unless the ray hits something, in which case it will stop where the ray hit.
However, when the object (with a 2D capsule collider) that is obstructing the 2D raycast is rotated to a certain angle, the hit is not detected anywhere but the object’s ends.
In case you don’t understand what I mean, here is a demonstration
Here is my script:
LineRenderer lr;
Vector2 startPoint;
Vector2 endPoint;
void Awake ()
{
lr = GetComponent<LineRenderer> ();
startPoint = lr.GetPosition (0);
endPoint = lr.GetPosition (1);
}
void Update ()
{
RaycastHit2D hit = Physics2D.Raycast (startPoint, endPoint - startPoint);
if (hit)
{
Debug.DrawRay (startPoint, hit.point - startPoint, Color.white);
}
else
{
Debug.DrawRay (startPoint, endPoint - startPoint, Color.white);
}
}
The start and end points are from a line renderer that I will use to actually show the ray in the game, and at the moment I am using it to set the start point and direction of the ray with two points.