Pointing a welded part to a end position while keeping its starting orientation?

I am trying to make this blue part (welded to the red part) point to the green part but maintain the red part’s orientation. It is somewhat working this is the code I am currently working with.

while wait() do
	workspace.TEST.START.Weld.C0 = CFrame.Angles(math.rad(workspace.TEST.START.Orientation.X),math.rad(workspace.TEST.START.Orientation.Y),math.rad(workspace.TEST.START.Orientation.Z)) * workspace.TEST.START.CFrame:ToObjectSpace(CFrame.new(workspace.TEST.START.CFrame.p, workspace.TEST.END.CFrame.p)) * CFrame.new(0, 0, -12)
	if workspace.TEST.STOP.Value then
		break
	end
end

At this angle it works fine.


But any other angle it moves offimage

I also want to be able to tell if the blue part is moving down or up to reach the green part how would I find that?

5 Likes

I’m afraid the solution is more complicated than getting the angles between the two parts. You’ll need to mess with CFrames.

Here’s two handy contructors: CFrameFromTopRight and CFrameFromTopFront. You’ll probably find yourself using these a lot:

-- Constructs a CFrame such that its position is p, 
-- its upVector is top, and its lookVector is front
function CFrameFromTopFront(p, top, front, right)
	if not right then
		right = front:Cross(top).unit
	end
	local top = top.unit
	local front = front.unit
	return CFrame.new(
		p.X, p.Y, p.Z,
		right.X, top.X, -front.X,
		right.Y, top.Y, -front.Y,
		right.Z, top.Z, -front.Z
	)
end

-- Constructs a CFrame such that its position is p, 
-- its upVector is top, and its rightVector is right
function CFrameFromTopRight(p, top, right, front)
	if not front then
		front = top:Cross(right).unit
	end
	local top = top.unit
	local right = right.unit
	return CFrame.new(
		p.X, p.Y, p.Z,
		right.X, top.X, -front.X,
		right.Y, top.Y, -front.Y,
		right.Z, top.Z, -front.Z
	)
end

If the three parts are named redPart, greenPart, and bluePart, then the CFrame of the bluePart will be this:

-- distance between red and green
local dist = (greenPart.Position-redPart.Position).magnitude
-- direction from red to green; bluePart will face this direction
local dir = (greenPart.Position-redPart.Position).unit
-- construct a CFrame whose up-direction is red's up direction and faces dir
local bluePartCFrame = CFrameFromTopFront(
    redPart.Position + dir*dist/2,  -- bluePart's position is in-between green and red
    dir:Cross(redPart.CFrame.rightVector.unit).unit, 
    dir
)

But you’re asking for a weld offset (and judging by the script in the op, a value for C0). The script above gives the WorldSpace CFrame (i.e. the actual CFrame for bluePart in the world), not a weld offset. For that, you’ll need to convert the CFrame above from world space to object space relative to redPart:

Assuming your weld’s name is weld, this is how you’d do it:

weld.C0 = redPart.CFrame:toObjectSpace(bluePartCFrame)

EDIT: I just realized that this solution was wrong. I’ve edited it. It might be right this time.

4 Likes

I accidentally fixed this issue on my own by using CFrame.Angles rather than CFrame.new(Start, End) to set the part’s orientation.

local greenPart = script.Parent.END
local redPart = script.Parent.START
local bluePart = script.Parent.Part
while wait() do
local Goal = redPart.CFrame:toObjectSpace(greenPart.CFrame).p
local Angle = -math.atan(Goal.Y /Goal.Z)
local Dist = 6
if Goal.Z < 0 then
Dist = -Dist
end
redPart.Weld.C0 = CFrame.Angles(Angle, 0, 0) * CFrame.new(0, 0, Dist)
end
2 Likes