ok so I’m back! Glad I got so frustrated attempting to learn this because now I have an understanding of what is going on, and WHY.
I only used the Laser gun casting ray developer pages to play around with how things worked I did not do any bullet shooting, only laser ray casting.
ViewportPointToRay is exactly what it says it is, sort of. It is shooting a laser from your eye balls (yes you sitting in the chair staring at the monitor) out to your mouse location.
*it has nothing to do with the gun tool location position at all, nothing.
Now do not count the distance your eye balls are from your monitor, negate that totally and stick your eye balls into your monitor and the laser is going from your eyes (Viewport) like Cyclops in the X-Men directly towards your mouse in the 3D world
Ok now we have that, lets figure out why it needs to do this.
In the end we need a ray coming out of our tool gun going out in the world towards our mouse position, this is the end goal.
But we do not know our MousePosition in the world so before we do anything with the gun we need to figure out our MousePosition in the 3D world.
This equation here is how everything is done.
FinalPosition = OriginPosition + DirectionVector
using this equation our MousePosition is the FinalPosition of the laser, that makes sense. So if we can figure out the other 2 variables then we are on our way.
Lucky that is exactly what ViewportPointToRay does.
ViewportPointToRay - the OriginPosition is where the camera or your eye balls in this case, are located in the world. So if you are zoomed out looking down on your character, picture 2 little eye balls way in the sky far back from your character, that is the OriginPosition.
ViewportPointToRay gives us the DirectionVector also which is the vector going from our eye balls out towards our mouse location in the 3d world.
so now we can calculate our MousePosition in the 3d world using our equation.
MousePosition = OriginPosition + DirectionVector
Ok awesome so we have our MousePosition in the 3D world, lets use it in combination with something else we know… we know our GUN TOOL location too!
Back to our equation
MousePosition = GunPosition + LaserVector
Obviously you see I substituted GunPosition for OriginPosition because you guessed it , that is the origin of where the laser should be coming out (not our eyes which are far back zoomed out on our character)
GunPosition is usually something like tool.Handle.Position
And I substituted LaserVector for DirectionVector because that is what it is in the world when we are doing ray casting with a laser.
so we have 2 of these variables known, MousePosition and GunPosition.
Lets solve for the unknown, the LaserVector, which is the actual laser that we want to be shooting.
LaserVector = MousePosition - GunPosition
the rest of it just has to do with seeing what intersects with our LaserVector to see what it will “hit”