When firing a ray, one of the mandatory arguments is the direction of said ray. Most people calculate the direction of a ray for guns using the following line of code:
(mouse.Hit.Position - gunPosition).Unit * range
Where;
mouse.Hit.Positon
is the Vector3 value of the CFrame of where the Mouse is hitting (intersection between the Mouse’s ray and a part)
gunPosition
is the position of the part on the gun from where the ray will be fired
range
is a positive finite number
Now, the goal of this line is to create the ‘directional vector’ of the ray. So, we can separate the line of code into two parts:
(mouse.Hit.Position - gunPosition).Unit
I’m not really sure what the math behind this is. I know the .Unit
is creating a unit vector with a specific direction, but how exactly is that direction calculated? How exactly is (mouse.Hit.Position - gunPosition)
calculating the direction of the unit vector?
* range
This is fairly simple, its a basic vector scalar multiplication which gives the unit vector a norm.
Thanks a lot!