I’m trying to understand how I can make a basic beam in raycasting.
Issue?
I don’t understand the math used in raycasting.
What have I tried?
I’ve looked at various raycasting resources which have shown the basics of raycasting but I want to adapt and grow my skills in raycasting. I’m also watching a video which highlights how to make a beam and I’m trying to understand the math functions.
Main questions:
-How does the create beam function in the video work?(in the raycasting section of the video)
-Is there any math, vector, or magnitude related topics I should know to improve in raycasting?
Specific questions:
-How does line 18 do or "part.CFrame = CFrame.new(midpoint, origin)"work in the video?
-Why is the midpoint = origin + direction/2?
“How does line 18 do or ‘part.CFrame = CFrame.new(midpoint, origin)’ work in the video?”
This is setting the part’s CFrame (Coordinate-Frame, a 10-parameter representation of position and rotation) to be at the midpoint, facing the origin. CFrame.new can take two parameters, a position CFrame.new(pos: Vector3), or a position and a vector to look at, CFrame.new(pos: Vector3, lookAt: Vector3).
“Is there any math, vector, or magnitude related topics I should know to improve in raycasting?”
I would look into learning more about Vectors, such as their properties and what they represent. Learning how to manipulate vectors is really powerful in terms of programming in general, as positions are represented by vectors.
For the mid point example you first start up by adding origin which is the position vector of the pistol/gun barrel. In the diagram above it is the blue vector going from world origin (0, 0, 0) towards P1 (variable).
Once you reach P1 you add direction which is a direction vector starting from P1 that goes to P2 which is usually the mouse/target position. This is usually calculated via direction = mousePosition - gunPosition (requires a different vector diagram to visualize this). This will end up with the purple vector in the diagram above causing the path to end up at P2 or mouse position.
Also in another one of B Ricey’s videos he says this method is deprecated, is there another method I should use to align and position the part properly?