Comments and answers for "Raycast between 2 points, how to get direction and length?"http://answers.unity.com/questions/697017/raycast-between-2-points-how-to-get-direction-and.html
The latest comments and answers for the question "Raycast between 2 points, how to get direction and length?"Comment by shopguy on shopguy's answerhttp://answers.unity.com/comments/697269/view.html
Thank you, this should help in future code. Going to accept the other answer only because I think it is optimized for this particular usage.Tue, 29 Apr 2014 15:01:50 GMTshopguyAnswer by robertbuhttp://answers.unity.com/answers/697045/view.html
To cast between two points you want [Physics2D.Linecast()][1].
if (Input.GetMouseButtonDown(0))
{
Vector3 p1 = Camera.main.ScreenToWorldPoint(LastMousePos);
Vector3 p2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D h = Physics2D.Linecast(p1, p2);
Debug.Log(p1 + " -> " + p2 + " = " + h.collider);
LastMousePos = Input.mousePosition;
}
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Physics2D.Linecast.htmlTue, 29 Apr 2014 07:46:37 GMTrobertbuAnswer by robhuhnhttp://answers.unity.com/answers/697042/view.html
Both Vectors (p1 and p2) are in world space. To get a local vector pointing to p2 **relative to p1** you need to subtract p1 from p2.
Vector3 p1 = Camera.main.ScreenToWorldPoint(LastMousePos);
Vector3 p2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 diff = p2-p1;
RaycastHit2D h = Physics2D.Raycast(p1, diff, diff.magnitude);
Didn't test it but it should work.Tue, 29 Apr 2014 07:32:13 GMTrobhuhn