How to create a part in the middle of two points factoring in the rotation of the first part?

I’m trying to create a plugin that you can create a part by dragging your cursor from point A to point B. I want it to factor in the CFrame rotation of the first part selected, but the part created doesn’t fit the points if the rotation is not divisible by 90 degrees.

Example of what I mean
6af6be5861de1147ba7e33afc46e126f
(Red is the start point, green is the endpoint, blue is the box.)

I know it’s the size messing up(specifically, while the distance between the two parts doesn’t change but the distance between the red part’s X versus the green part’s X DOES change), but I don’t know HOW to fix it.

Here’s my code, any suggestions would be a great help!

local runService = game:GetService("RunService")

local startPart = script.Parent.Start
local endPart = script.Parent.End
local glow = script.Parent.Glow
local rad = math.rad(15)
function withoutTranslation(cf: CFrame): CFrame
	return cf - cf.Position
end

local function partFromTwoPoints(startCF:CFrame,endPos : Vector3)
	local midPoint = (startCF.Position + endPos) / 2
	local CF = withoutTranslation(startCF) + midPoint

	local difference = (startCF.Position - endPos)
	local size = Vector3.one + Vector3.new(math.abs(difference.X),math.abs(difference.Y),math.abs(difference.Z))
	return CF,size
end

runService.Heartbeat:Connect(function(dt)
	startPart.CFrame *= CFrame.Angles(0,rad * dt,0)
	local cf,size = partFromTwoPoints(startPart.CFrame,endPart.Position)
	glow.CFrame = cf
	glow.Size = size
end)

NVM solved it

local difference = (startCF.Position - endPos)

becomes

local difference = startCF:PointToObjectSpace(endPos)

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