CFrame Angles will not look the same on a different part

Hello, my goal in this is to make a script that can be used to replicate CFrame Angles from one part to another while having both parts appear to be at the same Orientation.


Setup in workspace

image


Code

local red = workspace.a1
local blue = workspace.r1
local pi = math.pi
local inf = math.huge

for q = 1, inf do
	wait()
	
	local blue_cf = blue.CFrame
	local blue_look = blue_cf.LookVector
	
	local look = CFrame.Angles(blue_look.Y * pi / 2,blue_look.X * pi / 2,blue_look.Z * pi / 2)
	
	red.CFrame = CFrame.new(red.Position) * look
end

How could I correct this to make it work?

LookVector is not the same as rotation. If you want to reconstruct a rotation based on it, you can do this.

local look = CFrame.new(red.Position, red.Position + blue_look)

but it can’t duplicate the roll. You need a lot more data to perfectly mimic rotation, and you can get that this way.

local blue_matrix = {select(4,blue_cf:components())}
red.CFrame = CFrame.new(red.Position.X, red.Position.Y, red.Position.Z, unpack(blue_matrix))
1 Like

It worked, thanks!

Could you explain the fourth parameter in CFrame.new(), what unpack(), select(), and :components() do?

unpack takes a table and separates it.

local a, b, c = unpack({1, 5, 30})
print(b) --> 5

select selects a few things out of a list of arguments. It allows me to ignore the first three parts of the CFrame, the XYZ position.

print(select(3, "first", "second", "third", "fourth", "fifth"))
--> "third" "fourth" "fifth"

components gives me all 12 components of a CFrame.
X,Y,Z,R00,R01,R02,R10,R11,R12,R20,R21,R22
The R values are the rotation matrix, which is not as complicated as you might think, but it’s probably best to look it up since there are a lot of resources that explain it better than I would. But the gist of it is that there is, in that data, a Right vector, Up vector, and Forward vector. It’s like LookVector, but there’s three of them. This means that there is enough data to accurately describe rotation.

CFrame.new, as I created it, can be re-written like this.

CFrame.new(
    RedX, RedY, RedZ,
    BlueR00, BlueR01, BlueR02,
    BlueR10, BlueR11, BlueR12,
    BlueR20, BlueR12, BlueR22
)

There are about 10 different ways to use CFrame.new. CFrame.new is pretty powerful. Again though, best to look it up if you care.

2 Likes