How to get the angles between two objects

I am trying to get a part’s relative angles to another one’s but I can’t seem to find the right way to do it

https://gyazo.com/02642348d41024db25fe55467219ae82

as in the image in the link, I want to get the individual rotations that i have to do on each axis to get to the first part’s orientation

I tried to get the angles individually by separating each of the 3 axis’ vectors as following:

local p1  		= Part1.CFrame.LookVector   
local p2		= Part1.CFrame:ToObjectSpace(Part2.CFrame).LookVector
local ZPart1	= Vector3.new(P1.X,P1.Y,0)
local ZPart2	= Vector3.new(p2.X,p2.Y,0)
local Zangle 	= math.acos((ZPart1:Dot(ZPart2))/ (ZPart1.Magnitude * ZPart2.Magnitude))

but Zangle is always 90 degrees for some reason

Thanks,
Tem

1 Like

Check this post out from a few days ago.
It might help:

1 Like

Sadly, it doesn’t work for my case.
But thank you for the suggestion

Are you just trying to find the difference in terms of the ORIENTATION, or are you also accounting for the angle BETWEEN them (in world space)

hmm, how do I explain this?

let’s say we have a part with it’s look vector like this: V3(0,0,-1)
Now, I rotate (yaw) it 90 deg to the right, so now it’s LookVector is like this V3(-1,0,0)
And now, I pitch it to 90 deg counterclockwise. Now the LookVector is V3(0,1,0)

If i run the function with arguments p1 and p2, where p1 is the part after the rotations {V3(0,1,0)} and p2 is the part before the rotations {V3(0,0,-1) } the output would be something like this: V3(90,-90,0)

Are you trying to find the dot product between the two look vectors?

As I was writing this reply I have realised that i can’t do it with only LookVector. I need all the three vectors, LookVector, UpVector and RightVector to get all the angles

If you want to get the difference of the lookvector and can be used for UpVector ect…

local object1 = ??
local object2 = ??
local dotproduct = object1.CFrame.LookVector:Dot(object2.CFrame.LookVector)
local angle = math.deg(math.acos(dotproduct)) -- math.acos(dotproduct) if just in radians

that way you get the angle between the 2 vectors, not how, in local space, you have to rotate the second object to get the first one’s orientation