Explanation of A Script

Hello, :wave:

I made a simple script that makes a ray from a starting part to an ending part. It works and stuff, but I just want to understand it.

Code:

local startPart = game:GetService("Workspace").Start
local finishPart = game:GetService("Workspace").Finish


local rayCastParams = RaycastParams.new()
rayCastParams.FilterDescendantsInstances = {startPart, finishPart, game:GetService("Workspace").Baseplate}
rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist



while wait() do
	
	local ray = game.Workspace:Raycast(startPart.Position, (startPart.Position - finishPart.Position).Unit * (startPart.Position - finishPart.Position).Magnitude, rayCastParams)
	
	if ray then print(ray.Instance.Name) end
		
end

I really need an explanation of the script especially the ray variable. Thanks! :wave:

This code repeatedly casts a ray and prints the name of the part it hits if it finds one. Although I don’t see why you need to raycast at a rate this fast.

1 Like

Yeah ik, but I don’t understand anything in the ray variable:

local ray = game.Workspace:Raycast(startPart.Position, (startPart.Position - finishPart.Position).Unit * (startPart.Position - finishPart.Position).Magnitude, rayCastParams)
	

Those are just the raycast parameters. The first parameter is the starting point, the second is the direction, and the last is the settings for the ray.

1 Like

A ray object is apparently a line in the 3D-space. Visualize it as a line without any other dimensions, it’s simply one-dimensional. The parameters decides its direction and its length(I think). The parameters appears to consistently create a ray in the direction and length between start and finish.

1 Like

A ray is a one-way directional object. It is, indeed, one-dimensional. However, you can not define the length of a ray, as it goes infinitely in the direction determined by the second parameter. The parameters go as follows: Origin point, direction, ray cast parameters (various settings).

1 Like

The length could be determined through a factor, I do remember that you could limit its length through something. The detail actually explicitly mentioned the length of it matters. Only issue is I wasn’t certain if they actually meant it was a limitation or if the argument has to specify length?

The directional vector of the ray. Note that the length of this vector matters, as parts/terrain further away than its length will not be tested.

Further reading actually proves that the argument can define its length.

Note that the length (magnitude) of the directional vector is important, as objects/terrain further away than its length will not be tested. If you’re using a CFrame to help create the ray components, consider using CFrame.LookVector as the directional vector and multiply it by the desired length as shown in the example below.

Here’s the link:

1 Like

The first parameter is the Start Position, the second parameter it multiplies the unit (The direction between start and finish in 0 to 1) and magnitude (How many studs between start and finish) which is where the ray will be going and, the last parameter are the raycastparams basically like settings for the ray. Since it is on blacklist those parts inside the table will be ignored when the ray touches them. I hope you understand now.

1 Like

Oh thanks. That cleared some confusion. But I’m still wondering what the second parameter of the raycast function, which is direction. More specifically, I don’t understand the unit part.

Basically what Unit is is the direction between a Position for example say print(workspace.Part.Position.Unit) would print out the direction from the position 0, 0, 0 and the part’s position in vector3 form or print(workspace.Start.Position - workspace.Finish.Position).Unit would the same thing


Since the start is pointing towards the negative Z axis

2 Likes