I’ve almost got a working code but it doesn’t work at an angle
As you can see the small parts are still inside the big parts because of the way it works.
Here’s what I currently have and I’m unsure how I would fix this.
part0 = workspace.r
part1 = workspace.o
local Part0ClosestToPart1 = CFrame.new(part0.Position+CFrame.new(part0.Position,part1.Position).LookVector*part0.Size/2)
local Part1ClosestToPart0 = CFrame.new(part1.Position+CFrame.new(part1.Position,part0.Position).LookVector*part1.Size/2)
local p0 = Instance.new("Part",workspace)
local p1 = Instance.new("Part",workspace)
p1.CFrame = Part0ClosestToPart1
p0.CFrame = Part1ClosestToPart0
p1.Size = Vector3.new(.5,.5,.5)
p0.Size = Vector3.new(.5,.5,.5)
p1.Name = "p1"
p0.Name = "p0"
I’m guessing the issue is .LookVector*part0.Size/2 and .LookVector*part1.Size/2
local partA = workspace.A
local partB = workspace.B
local posA = partA.Position
local posB = partB.Position
local dir = posB - posA
local rayParam = RaycastParams.new()
rayParam.FilterDescendantsInstances = {partA, partB}
rayParam.FilterType = Enum.RaycastFilterType.Whitelist -- Raycast is only limited to the 2 parts
local ray1 = workspace:Raycast(posB,-dir,rayParam) -- Raycasts from partB to partA
local ray2 = workspace:Raycast(posA,dir,rayParam) -- Raycasts from partA to partB
local p1 = Instance.new('Part',workspace)
p1.Position = ray1.Position
p1.Size = Vector3.one/2
p1.Color = Color3.new(0,1,0)
local p2 = Instance.new('Part',workspace)
p2.Position = ray2.Position
p2.Size = Vector3.one/2
p2.Color = Color3.new(1,0,0)
Using a raycast wouldn’t work for me since they wouldn’t aim for the closest position but rather the middle
the line is the raycast and the small parts is the method im trying to make work. As you can see the way I’m doing it would get the position closest to the other part which is what I need
Nah raycasts would work, he just did the whitelisting part incorrectly, raycasting would make this system much simpler and compatible with various shapes.
If you want to use your method, you can maybe divide the unit direction by the maximum number of its axis, in other words make one of the axis of unit direction be 1.
local partA = workspace.A
local partB = workspace.B
local posA = partA.Position
local posB = partB.Position
local dir = posB - posA
local sizeA = partA.Size
local sizeB = partB.Size
local unitDir = dir.Unit
local dirMax = math.max(math.abs(unitDir.X),math.abs(unitDir.Y),math.abs(unitDir.Z))
unitDir /= dirMax
local pos1 = posA + unitDir*sizeA/2
local pos2 = posB - unitDir*sizeB/2
local p1 = Instance.new('Part',workspace)
p1.Position = pos1
p1.Size = Vector3.one/2
p1.Color = Color3.new(0,1,0)
local p2 = Instance.new('Part',workspace)
p2.Position = pos2
p2.Size = Vector3.one/2
p2.Color = Color3.new(1,0,0)
I used your method except i directly took the “LookVector” of the direction