CFraming relative to part, maintaining rotations?

Hey guys,

So I’m kind of stuck with some basic CFrame related mathematics. If I had a theoretical part in the world that is rotated on multiple variable axes, and I want to move it forwards using CFrames where forwards is relative to the part, how would I do that?

See the picture attached:

I want to move the part in the direction of the arrow, while maintaining the Y level in the world and the rotation of the part.

I could just move it forwards and adjust the Y level relative to the world in a second step, but I want to be able to smoothly transition it forwards, and the Y rotation could theoretically be anything.

1 Like

For this problem, you can just get the look vector of where the part is facing and times it by (1,0,1)

local part
local moveForwardsOnXZPlane = part.CFrame.LookVector*Vector3.new(1,0,1)
part.CFrame = part.CFrame + moveForwardsOnXZPlane

Otherwise, if you are looking for movement on a plane other than the XZ plane then you might need to resolve the vector differently.

2 Likes

It’s not a perfect solution but it puts me forward enough that I have a couple ideas. Thanks!

1 Like

This is the best way to do it IMO and it’s how I’ve done it in the past. I would make 2 changes to the implementation.

  1. Unitize moveForwardsOnXZPlane before you use it.
  2. This won’t work if the LookVector is perfectly up or down. Check that the absolute value of its dot product with <0, 1, 0> is less than 0.0001. If that’s the case, then we know it’s looking pretty much straight up or down, so use the UpVector instead.
1 Like