I'm confused about some things in raycasting.
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
The first argument is start, and the second is offset. The start is where the ray is coming from and the offset is how far to check off of the start. Here are my questions:
Why subtract tool.Handle.CFrame.p
from mouse.Hit.p
? Isn't the goal to create a ray between the handle and the mouse? If so, why are we subtracting things from the mouse?
What does .unit do what why multiply it by 300?
local hit, position = game.Workspace:FindPartOnRay(ray, user) --sutff here local distance = (position - tool.Handle.CFrame.p).magnitude --stuff here rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)
So, this is CFrame.new(startPosition, lookAt)
. We're making the ray start at the position the hit was at, and point towards the tool. So,
1. When we move from one point to another, we move a certain distance in a certain direction. That quantity is a vector (a distance in a particular direction). The vector meaning the path from one point to another is called the displacement from the start point to the end point.
The displacement from point A
to point B
is B - A
.
This makes sense since if we start at A
, we move the displacement, and we should get at B
. That is, A + D = B
, so D = B - A
.
So to find the direction and distance from from
to to
, we do to - from
.
2.
A unit vector is a Vector of length one (the unit). .unit
turns the displacement into a direction by ignoring how long it is. We change it into something we know is length 1
. By multiplying it by 300, we get something that we now know is length 300 but still in the direction from the tool to the mouse.
3. The math works out that way because of the matrix definition of CFrames.
A clearer way to write it would be:
rayPart.CFrame = CFrame.new( (position + tool.Handle.CFrame.p) / 2 , tool.Handle.CFrame.p)
Remember,startPosition
is the center of the part, not one of its ends. We want the part to span from position
to the tool (tool.Handle.CFrame.p
). So the center is actually halfway,length / 2
, from either position
or tool.Handle.CFrame.p
!
The * CFrame.new(0,0, -length/2)
essentially moves the CFrame forward length/2
studs, which puts one end of the part at position
.
Hopefully this is all clear enough, I can edit this to improve anything if you want something better explained
Locked by Perci1, Shawnyg, and BlueTaslem
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?