How can I find if a raycast has passed through two points?

29615-example.png so I can tell if I have already had my character go that way so he can choose another way to go. Is this possible? Thanks in advance - you people are the best!

If you look at my image, I would like to detect if a line from C to D passes between points A and B at the intersection where the red X is.

You could create a mesh collider (plane) on a run between A and B with some height (must be relatively high depending on flatness of your level).

  1. Raycast C-D
  2. On Found D create mesh collider
  3. Raycast Again same direction
  4. If hit your mesh set some flag or continue with your code.

Just a theory never tried it.

You’re probably best off looking for a 3D line to line collision test off Google and porting it to Unity (unless you need a 2D test, which is faster to calculate).

See if this works for you (ported from StackOverflow)

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

	
	bool LineLineIntersection3D(
		Vector3 LineA_Pt1,
		Vector3 LineA_Pt2,
		Vector3 LineB_Pt1,
		Vector3 LineB_Pt2, out Vector3 IntersectionPt)
	{
		Vector3 da = LineA_Pt2 - LineA_Pt1; 
		Vector3 db = LineB_Pt2 - LineB_Pt1;
		Vector3 dc = LineB_Pt1 - LineA_Pt1;
		
		if (Mathf.Abs( Vector3.Dot(dc, Vector3.Cross(da,db) ) ) > Mathf.Epsilon ) // lines are not coplanar
		{
			IntersectionPt = Vector3.zero;
			return false;
		}
		
		float s = Vector3.Dot( Vector3.Cross(dc,db),Vector3.Cross(da,db)) / Vector3.Cross(da,db).sqrMagnitude;
		if (s >= 0.0f && s <= 1.0f)
		{
			IntersectionPt = LineA_Pt1 + da * s;
			return true;
		}
		IntersectionPt = Vector3.zero;
		return false;
	}

	// USAGE EXAMPLE
	void Awake()
	{
		Vector3 v3Intersection;
		if( LineLineIntersection3D( 
	                new Vector3( -1.0f, 0.0f, 0.0f ),  	// Point A
		        new Vector3( 5.0f, 0.0f, 0.0f ),	// Point B

	         	new Vector3( -1.0f, 0.0f, 3.0f ),	// Point C
       	        new Vector3( 5.0f, 0.0f, -7.0f ), 	// Point D
           		out v3Intersection ) )
		{
			Debug.Log( "Intersection at " + v3Intersection.ToString( "F4" ) );
		}
	}
}