Raycast between 2 points, how to get direction and length?

With this test code (which is wrong):

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Vector3 p1 = Camera.main.ScreenToWorldPoint(LastMousePos);
        Vector3 p2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D h = Physics2D.Raycast(p1, p2);
        Debug.Log(p1 + " -> " + p2 + " = " + h.collider);
        LastMousePos = Input.mousePosition;
    }
}

What should I be using for p2? I wish Raycast() had a version that took source + destination, but instead it wants direction (and optionally distance). So starting with source + destination, how do I get direction and distance?

I want the above code to find any collider between the last 2 mouse clicks. It works (in limited test cases I’ve tried) going in one direction, but not the other.

This is a 2D game. I want to detect any objects hit as the user drags the mouse across the screen. I know how to check for a hit at the current mouse location, but I also want to catch times when they drag over an object really fast (before the next Update/frame). I guess I’ll probably change to RaycastAll in my final code, but shouldn’t matter for sake of this question.

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.

To cast between two points you want Physics2D.Linecast().

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;
}