How to turn look vector into a rotational cframe

I tried looking on CFrame for a way to do this but i am stuck. Also it is not relative to any part just the workspace.

The reason why i need this is so i can restrict the movements of a turret im making. For example i get teh look vector, then clamp the y value, but then i need to turn it back into a cframe.

Check this post I believe it might help Limit CFrame Rotation - #5 by IdiomicLanguage

1 Like

Yes, that post will help a lot.

A quick note here for OP though, a lookvector alone is not enough to create a CFrame. A look vector is the forward direction, but what is the up direction?

I am guessing you want to use the opposite of gravity as up, or <0, 1, 0>. This is fine when the look direction is horizontal, but if it points up or down a little, then our CFrame up direction will change.

The solution is to find an up direction that is orthogonal to both our right and forward directions. We can get the right direction by finding a vector that is orthogonal to both our forward and the global up direction.

I am on my phone so I can’t write code, but it looks like this:

gUp = <0, 1, 0>
right = lookvector:Cross(gUp)
up = lookvector:Cross(up)

You can then throw the lookvector, right, up, and a position into a CFrame constructor. I forget which it is though, it is fairly new.

Best of luck!

1 Like

A lookvector isn’t enough to make a CFrame, you also need the upvector, which, assuming the turret will never point up or down, will be 0, 1, 0. You can then use CFrame.fromMatrix to make a CFrame.

Example:

local UpVector = Vector3.new(0, 1, 0)
local TurretCF = CFrame.fromMatrix(Turret.Position, LookVector, UpVector)
1 Like

Thank you, I looked at this:

however i found it a little confusing as to how it works. Can you please explain it step by step?

Also i think i found a way by doing

local part = workspace.Part
local rotationAndPosition = CFrame.new(part.Position, part.CFrame + lookVector* part.Position)
local cFrameFromLookVector = rotationAndPosition - rotationAndPosition.Position

Does this work?

1 Like