Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make a part face the same direction as another part but only on the Y axis?

Asked by 7 years ago

What I want to do is make a part face the same direction as another part, what I've tried doing is this:

Part1.Orientation = Part2.Orientation

This works, but it also copies every axis. I only want the part to be copying the Y axis of the other part. How do I do this?

1 answer

Log in to vote
2
Answered by
2eggnog 981 Moderation Voter
7 years ago
Edited 7 years ago

There are multiple ways to do this using CFrames, but here's a fairly intuitive one:

Part1.CFrame = CFrame.new(Part1.CFrame.p, Part1.CFrame.p + Part2.CFrame.lookVector * Vector3.new(1,0,1))

For a CFrame constructed from two Vector3 arguments, the first argument will be the position, and the second is the point the part should face towards. In this case, the point the part should face towards is created from the direction Part2 is facing on the XZ-plane (which is the plane affected by a rotation around the Y axis).

If you're familiar with matrices, this way is (negligibly) more accurate, albeit less intuitive:

local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = Part2.CFrame:components()
Part1.CFrame = CFrame.new(Part1.Position.x, Part1.Position.y, Part1.Position.z, R00, 0, R02, R10, 1, R12, R20, 0, R22)

In this approach, the CFrame is constructed from every one of its components (three positional components and the nine rotational matrix entries.) The rotation matrix is copied from Part2 except for the Y column, which is replaced with the Y unit vector (0,1,0).

Ad

Answer this question