I am attempting to make a part always be forced on another parts realtive axis using CFrame. My objective is to make these squares in the middle of my track always be in the middle, but still be able to move them up and down the track. Essentially, locking the square on the tracks relative X axis.
I have an example of what I want to achieve here, however I need this to occur on all rotations:
Currently this just updates the X position of the square to the X position of the track, which would obviously not work on rotated parts.
So far I have it automatically update the sqaures rotation, but would also like it to stay in the center as shown above. How may I go about doing this?
My best guess to tackling this situation is to make an invisible, non-colliding nodes track. When the parts are moving, fire a ray downwards to retrieve which node they are on, then have them align themselves to the Contact Point returned by the raycast.
Have the node’s front face point towards the direction they should be moving towards. (Can be used for rotation)
You can take the vector from the track location to the square part (part.Position - track.Position) and project it onto the track’s x-axis, then subtract that projection from the part position. It would look something like: