How can I get the “direction” (such as front, back, right, and left) of an object relative to another object. So lets say I want to get the rotation of Part A, relative to Part B. If Part B’s orientation changes so does the 0 degree axis for Part A.
I calculated global directions with the code shown below, but how can I get the rotation of one object relative to another?
local v = Camera.CFrame.LookVector
print(math.floor(math.deg(math.atan2(v.X, v.Z))+180))
Also, in my case I am trying to get the relative rotation of the Camera around the HumanoidRootPart in player.
Here is a quick image I drew up to help explain my situation:
I believe EgoMoose has a great explanation on the inner workings of this method on Scripting Helpers but Roblox has a built-in function for object space calculation. Using CFrame.toObjectSpace() or CFrame.toWorldSpace(), you can retrieve a relative rotation.
Unfortunately I couldn’t find EgoMoose’s example, but this one should be able to help you. I’ll do my own research and see if I can get you the mathematics.
EDIT: Knowing what I do about math, I understand you can get the relative position of an object by subtracting the position of one object minus the other. I would recommend trying that with an Orientation vector (or a CFrame) and then seeing if that works.
EDIT #2: Turns out that’s exactly what toObjectSpace() does…