Set part CFrame and Size to that of Ray

This might sound kind of confusing but I want to make it so the green lines between the green dot and the start position are collideable and make it so the ray gets cut at the hit position.

To do this I want to have it so the “TemplateDot” CFrame and Size is the same as the CFrame of the projected ray. Any help is appreciated.

Here’s an image of what the ray looks like as of right now for context:

How the workspace is setup:
image

--<< NotFrindow >>--

local projector = script.Parent

--<< Raycast Params >>--

local ignoreList = {}

for _ , v in ipairs(workspace:GetDescendants()) do
	if v:IsA('BasePart') and v.Transparency == 1 then
		table.insert(ignoreList, v)
	end
end

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = ignoreList

print("D.O.T.S Projector Ignore List:")
print(ignoreList)

--<< Functions >>--

function castDot(direction)
	local raycastResult = workspace:Raycast(projector.Position, direction, params)
	RaycastParams.new()
	
	local newDot = projector.TemplateDot:Clone()
	newDot.Parent = projector.Dots
	newDot.StartPoint.WorldPosition = projector.Position
	newDot.EndPoint.WorldPosition = raycastResult.Position
end

for i = 1, 200 do
	wait(0.05)
	castDot(Vector3.new(math.random(-180, 180), math.random(0, 180), math.random(-180, 180)))
end
1 Like

Do you mean like turn it into a part?

1 Like

That’s essentially what I’m trying to do, yes. The part I’m trying to change the size and CFrame of is the “TemplateDot” as seen in the explorer.

You can do CFrame.lookAt from the start to the end of the ray. Then you need to change the Position of that cframe to be the middle of the ray. This is the CFrame your part should have.

1 Like

How exactly would this be put into a script?

--<< NotFrindow >>--

local projector = script.Parent

--<< Raycast Params >>--

local ignoreList = {}

for _ , v in ipairs(workspace:GetDescendants()) do
	if v:IsA('BasePart') and v.Transparency == 1 then
		table.insert(ignoreList, v)
	end
end

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = ignoreList

print("D.O.T.S Projector Ignore List:")
print(ignoreList)

--<< Functions >>--

function castDot(direction)
	local raycastResult = workspace:Raycast(projector.Position, direction, params)
	RaycastParams.new()
	
	local newDot = projector.TemplateDot:Clone()
	newDot.Parent = projector.Dots
	newDot.StartPoint.WorldPosition = projector.Position
	newDot.EndPoint.WorldPosition = raycastResult.Position
	newDot.CFrame = CFrame.lookAt(newDot.StartPoint.WorldPosition, newDot.EndPoint.WorldPosition)
	newDot.Size = Vector3.new(raycastResult.Distance, 0.001, 0.001)
end

for i = 1, 200 do
	wait(0.05)
	castDot(Vector3.new(math.random(-180, 180), math.random(0, 180), math.random(-180, 180)))
end

Using the new code I wrote above, it came out like this:

CFrame.lookAt creates a CFrame whose position is the first position given. You need to change the position to be the center of the ray:
newDot.CFrame = newDot.CFrame - newDot.CFrame.Position + (StartPoint + EndPoint) * 0.5

Right, but where would CFrame.lookat come in?

In my example newDot.CFrame is the CFrame you calculated from lookAt.

You can also just do it this way:
newDot.CFrame = CFrame.lookAt((startPoint + endPoint) * 0.5, endPoint)

This is definitely a step closer but the physical part and the ray bisect each other:

Swap your X and Z axes for size

How did I not think of this :sob: preesh tho

1 Like

Here’s the final product! Made possible with the help of @azqjanna and @7z99.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.