How to orient a part to 2 points using a CFrame

  1. What do you want to achieve?
    I want to orient a part to 2 points using a CFrame.

  2. What is the issue?
    I don’t know how to do it.

  3. What solutions have you tried so far?

local point1 = workspace.point1
local point2 = workspace.point2

local part = workspace.Part

part.CFrame=CFrame.new(part.Position,point2.Position)

Got this:
Screenshot 2024-02-12 175250
But i need this:
Screenshot 2024-02-12 175359

3 Likes

The problem is that when using, look at to set CFrame it makes that the Lookvector is facing the seccond position. One option is that you change the scale of the part so the X Size is swapped with the Z Size of the Part.

2 Likes

Can you explain in more detail, I don’t understand how that helps me?

2 Likes

AHA, i figured it out, because i have no hobbies, i rebuilt what you have built. You need to multiply the CFrame with this:

* CFrame.Angles(0,math.rad(90),0)

But of course dont copy the * thing, if you already typed it!

3 Likes

This may work in that specific situation, but I don’t think this solution is applicable to every configuration of positions.

3 Likes

Are you assuming at the start that (point1.Position - part.Position) and (point2.Position - part.Position) are orthogonal, or are they arbitrary points in space? And is this also assuming that the Part should turn in place, with part.Position not changing?

The case of already orthogonal “handles” is pretty easy, and even if the alignment is not perfect, when you assign the CFrame to part.CFrame, orthonormalization that’s done by the Roblox engine will fix small numerical issues for you.

local right = (point2.Position - part.Position).Unit
local up = (point1.Position - part.Position).Unit
local look = right:Cross(up)
part.CFrame = CFrame.fromMatrix( part.Position, right, up, look )

I don’t know which sides of your grey part are front vs back, left vs right, etc., so you might have to change the sign of ‘right’ and/or ‘up’ to get what you’re after.

The case of doing a “best fit” sort of alignment to 2 arbitrary points in space is more work, and depending on your use case, it may be desirable to give one point priority (e.g. align perfectly to point1 first), and then project the second point onto the plane orthogonal to (point1.Position - part.Position) and align to that. Kinda depends on what you’re trying to do, exactly.

4 Likes

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