I have watched tutorials and tried practising by myself a lot but it doesnt really seem to get lodged into my brain. Should i just keep practising more? How did you guys learn raycasting?
I learned raycasting from a tutorial Roblox had on their wiki back in 2014-2015, it was a simple laser gun tutorial where they took the paintball gun model and turned it into a raycasting laser. lol
It took about 5 attempts for me to actually have an understanding to what raycasting is. I think it’s best if you just research more and look at examples to get the hang of it!
How about a lesson right now? (yay free learning maybe)
Basically rays are 1 dimensional objects(or lines) that start from one point and then extends to another.
This picture represents the code:
local Ray = Ray.new(Vector3.new(0, 0, 0), Vector3.new(1, -1, 0)
Now this picture will show how this block of code works:
local Parts = workspace:FindPartOnRay(Ray, Part1) -- The first parameter(Ray) is the ray, the second parameter(Part1) is a parameter to tell the ray to ignore that object and it's descendants
if #Parts > 1 then
for i, v in pairs(Parts) do
print(v) -- prints only part 2
end
end
A raycast, you can think about it like a half line which we cast in 3D space. but the great thing about this half line is that you can check if it collides with something. in another way if there is anything on it’s way.
first of all you create a ray object like this local ray = Ray.new(Origin, Direction*Length)
where Origin is the start position, Direction is a unit vector respresenting where the ray is looking at. Length represents the length of that ray or that line.
After we created our ray object that holds all the data needed. We need to cast it into our 3d world. which means we need to use the workspace. since the workspace is our game’s world where all the physics and rendering happens. So luckly roblox gaves us a collection of functions inside the workspace that it’s called FindPartOnRay, FindPartOnRayWithWhitelist…
But in this case i’ll be explaining just the first one. You need to do the following: local part, position, normal = workspace:FindPartOnRay(ray)
That function needs to be fed with 3 parameters, in this case we just passed the first parameter which is the ray. and in return it gives us a tuple of 3 variables which is the first part that been hit. the position where the ray hitted that part and the normal. which is a perpendicular vector to the surface that been hit.
I suggest reading this official roblox documentations:
Also i suggest watching the video mentionned by @Quoteory to understand vectors. because rays are just a representation of vectors but with an origin. with the ability to detect the entities in it’s way.
since workspace:Raycast() works now, you can do that as well. I prefer it because you can give the arguments in any order since it’s all under 1 table. You can also get the result in any order.
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = true
raycastParams.FilterDescendantsInstances = {}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local function raycastTo(startPosition, endPosition)
assert(typeof(startPosition) == 'Vector3')
assert(typeof(endPosition) == 'Vector3')
local raycastResult = workspace:Raycast(startPosition, (endPosition - startPosition), raycastParams)
return raycastResult.Instance or nil,
raycastResult.Position or nil,
raycastResult.Normal or nil,
raycastResult.Material or nil
end)