Rotating a part to face an object on different axis

I am currently facing a problem in making a part/union face an object, even on a different axis. My goal is to make a compass where the needle points to a SpawnLocation/Part.
The problem is the player holds the compass like this so you can read the needle:

I weld the needle to axis, then weld the axis to the main frame.
I have tried using CFrame.LookAt() to make the needle rotate, but that results the player glitching out and being flung.
In addition, the left side of the needle points at the part, not the red painted part, even though I have rotated the pivot point to make the red part the front.
(Trying to rotate the needle on only the Z axis has failed)

A video is here:

Thank you for replying!

Thanks for the sigma background music.
As for your problem, it’s gonna take a bit of time:

  1. get the vector from the compass to the spawn
  2. get the compass plane on which you want to make the compass pointer stay on
  3. project the vector from point 1) onto your plane from point 2), and get the vector on that plane
  4. apply the new vector from point 3) to your compass pointer.

Wish I could give you a code snippet, but it really takes a bit of time. I could guide you through each of thee steps, eventually.
Maybe the real problem here is point 2, since it’s a bit harder to get the plane, but this can be done by looking the compass’ orientation in space, writing an analytical expression for it. Consider this is a calculation that’s to be made each pos update (better not do it every heartbeat).

Also, this is my own way of doing it. I really have no clue if there’s a simpler way.
Now, while typing this I’ve thought of another possible method: place a huge invisible part from your spawn to the sky and raycast from the compass (I mean the compass’ plane, since you need to keep the orientation) all around till you find the vertical part, then point the compass’ pointer in that direction. This is probably much more memory expensive. Could be easier if done smartly.

Thank you for your detailed reply, I will probaby look for a simpler way, like making a duplicate needle that is placed on the same axis as the part, then copying the rotation data from there and pasting it into the main needle.

PS. There is no background music on my side, and thanks for the very detailed and professional answer!

Here’s my best recreation of the process in MS Paint (I can’t afford anything else):

The cloned needle will probably be invisible and be held in place somehow.

You probably might want to have a look at dot product for your problem

This is from unity but should explain it well

Well that’s pretty much what I was thinking of, although without the clone. The point 2 in my message was exactly the “cloned needle” in your drawing. Then you project it on the plane of the compass, meaning you rotate it by a certain degree to make it fit perfectly! The dot product the user above has sent is a way to make the projection happen.
Your drawing is great!
Also, I’m sure I heard some music in your video, although I can’t confirm anymore since for some reason it won’t replay. Doesn’t matter! Cheers.

Thank you! I will get to work implementing both of the solutions as soon as possible, I will list MasuroOshiro’s post as the solution because it seems the most efficient, but your post is a great solution too.

1 Like