- Home /
Check if two vectors are intersected
Hi guys, currently I am stuck in my game. I need to check if vector between start and end position of a game object (object is moved with drag and shoot) has collided with vector between other two game object's positions. I've tried LineLineIntersection method, but that seems to always return true (I'm guessing that for some reason it does not take length of a vector into a consideration). Arguments for a method 1. Variable to store the intersection point, 2. Start position of line 1, 3. Direction of line 1, 4. Start position of line 2, 5. Direction of line 2
public static bool LineLineIntersection(out Vector3 intersection, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2)
{
Vector3 lineVec3 = linePoint2 - linePoint1;
Vector3 crossVec1and2 = Vector3.Cross(lineVec1, lineVec2);
Vector3 crossVec3and2 = Vector3.Cross(lineVec3, lineVec2);
float planarFactor = Vector3.Dot(lineVec3, crossVec1and2);
//is coplanar, and not parrallel
if (Mathf.Abs(planarFactor) < 0.0001f && crossVec1and2.sqrMagnitude > 0.0001f)
{
float s = Vector3.Dot(crossVec3and2, crossVec1and2) / crossVec1and2.sqrMagnitude;
intersection = linePoint1 + (lineVec1 * s);
return true;
}
else
{
intersection = Vector3.zero;
return false;
}
}
Your answer
Follow this Question
Related Questions
Get Intersection of a Line and a Circle? 4 Answers
slowly rotate a object *need quick fix* 0 Answers
Normalizing vector doesn't give a constant direction. 2 Answers
How to rotate an object to face the direction it's going? 1 Answer
Controlling roll rotation when travelling along bezier curves 1 Answer