How to Rotate a Vector

There’s a fly script I’m working on where an AlignOrientation orients a character’s HumanoidRootPart based on their camera LookVector.

local camlookat = camcframe.LookVector
alignOrientation.CFrame = CFrame.new(rootpart.CFrame.Position, rootpart.CFrame.Position + camlookat)

It works, but the problem is that it makes the front of the HumanoidRootPart face the LookVector of the Camera, while I want the top of the HumanoidRootPart to face the camera LookVector whilst the front faces the negative UpVector of the Camera.

Basically I want the player to fly head-first, face-down.

I tried rotating the LookVector on an axis using the function in this post, but the results are really wonky

How may I achieve what I’m looking for?

You could just plus it with a cframe.angles, heres an example:

local camlookat = camcframe.LookVector
alignOrientation.CFrame = CFrame.new(rootpart.CFrame.Position, rootpart.CFrame.Position + camlookat) * CFrame.Angles(math.rad(-90),0,0)

With this, the alignorientation will look to the camera look vector, but with the top of the humanoidrootpart being rotated to the front, as you want.

Hope it helps!

If this is the case, this is pretty much the answer to your question, we can just create a new rotation matrix using these vectors.

Does this give you what you’re looking for?

local up = camera.CFrame.LookVector
local negLook = camera.CFrame.UpVector
local right = up:Cross(negLook)

alignOrientation.CFrame = CFrame.fromMatrix(
	root.Position,
	right,
	up,
	negLook -- the z vector is flipped in Roblox so the look vector is negative Z axis
)

I tried this already but for some reason it flips when looking downwards

Nice! This almost worked, but not quite.

Here’s the behavior:

Basically, when looking anywhere up-ish, it works as intended. When looking anywhere down-ish, it does the opposite of what intended…

Any ideas as to what causes this?

Oh that’s interesting, could you provide your script or a repro file? It’s working as expected for me

Medal_KJy5wSPm1h

Or here’s what my implementation looked like if you want to gloss over it:

local camera = workspace.CurrentCamera

local root = script.Parent:WaitForChild('HumanoidRootPart')
local attachment = Instance.new('Attachment', root)

local alignOrientation = Instance.new('AlignOrientation')
alignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
alignOrientation.RigidityEnabled = true
alignOrientation.Attachment0 = attachment
alignOrientation.Parent = root

local alignPosition = Instance.new('AlignPosition')
alignPosition.Mode = Enum.PositionAlignmentMode.OneAttachment
alignPosition.RigidityEnabled = true
alignPosition.Attachment0 = attachment
alignPosition.Position = root.Position + Vector3.yAxis * 3
alignPosition.Parent = root

game:GetService('RunService').RenderStepped:Connect(function()
	local up = camera.CFrame.LookVector
	local negLook = camera.CFrame.UpVector
	local right = up:Cross(negLook)

	alignOrientation.CFrame = CFrame.fromMatrix(
		root.Position,
		right,
		up,
		negLook -- the z vector is flipped in Roblox so the look vector is negative Z axis
	)
end)
1 Like

Gosh thanks!

Indeed the problem was on my end there, I was applying the CFrame incorrectly, like this

alignOrientation.CFrame = CFrame.new(rootpart.CFrame.Position, rootpart.CFrame.Position + (camfixedcframe.LookVector))

Now that I’ve done it as you did

alignOrientation.CFrame = camcframe

It works. Thanks again!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.