How do i cast ray from point to mouse position. I have bullet tracer that cast mesh from gunpoint to mousePos, if it hits collider it stops at hit.point. Somehow my ray goes thru mouse position in mouse direction.
In your first attempt you correctly gave the raycast method a direction but you didn’t specify the distance and so it casts a ray with an infinite distance and hits the box. In your second attempt you incorrectly gave the raycast method a world position instead of a direction. You also used the 3D version of Raycast which won’t work in 2D. You’ll probably find Linecast more suitable for what you’re trying to do. You can give it the world positions instead of having to calculate the direction and distance.
You should also note, 2D physics uses Vector2 not Vector3. Unity has to convert it to a Vector2 before passing to calls that require Vector2. You’re better off using rhe correct type rather than having it implicitly converted for you because using Vector3 can lead to subtle and hard to track bugs in your code such as when calculating distance when Z isn’t zero.