Here is a script I put together that will help with understanding how to do this.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAppearanceLoaded:Wait()
local Mouse = Player:GetMouse()
local SubPart = game:GetService("ReplicatedStorage").SS
local function CreateParams(List, Type)
local params = RaycastParams.new()
params.FilterDescendantsInstances = List
params.FilterType = Type
return params
end
local function CalculateDisatance(p1, p2)
local dist = (p1 - p2).Magnitude
return dist
end
local function CastAndFind(HitPos, Orign, Direction, Obj)
print(Obj)
local Cd
local FirstHit = HitPos
local SecondHit
if Direction == nil then
Cd = CFrame.new(Orign, HitPos).LookVector
else
Direction = Cd
end
local p = CreateParams({Obj}, Enum.RaycastFilterType.Whitelist)
local WayLong = CFrame.new(Orign,HitPos) * CFrame.new(0,0,-100)
local result = workspace:Raycast(WayLong.Position, (WayLong * CFrame.new(0,0,10000)).Position, p)
print(result)
if result ~= nil then
print("Pass")
SecondHit = result.Position
end
local Distance = CalculateDisatance(FirstHit, SecondHit)
local newCF = CFrame.new((FirstHit + SecondHit) / 2, SecondHit)
local NewP2 = SubPart:Clone(); NewP2.Parent = workspace; NewP2.CFrame = newCF; NewP2.Size = Vector3.new(.2,.2,Distance)
print("P1")
print(FirstHit)
print("------------------------")
print("P2")
print(SecondHit)
print("------------------------")
print("Distance")
print(Distance)
end
Mouse.Button1Down:Connect(function()
CastAndFind(Mouse.Hit.Position, Character:WaitForChild("HumanoidRootPart").Position, nil, Mouse.Target)
end)
We first start by getting all the variables needed, mouse hit, mouse hit position, the origin. The main part is how to find the two points. Using Mouse.Hit.Position will work for finding the first position. Finding the second will be more difficult though. By using some CFrame we can get the CFrame look of the Origin and make it so it is far away, then I used RayCast to find where the point that intersects between the first hit and the second hit. Any questions let me know.