Rotating a CFrame to a surface normal

Hello everyone. I wanted to know how I could rotate a CFrame to a surface normal.

function Camera:AlignToSurface(result: RaycastResult)
	self.Instance.CFrame *= CFrame.fromRotationBetweenVectors(
		self.Instance.CFrame.UpVector,
		result.Normal
	)
	
	self.Instance.CFrame =
		self.Instance.CFrame.Rotation + result.Position + result.Normal
	
	self.OrthagonalBasis = self.Instance.CFrame.Rotation
	self.Yaw = 0
	self.Pitch = 0
	self.Roll = 0
end

This is a function I’m using in a custom Camera class.

Note that: self.Instance = workspace.CurrentCamera

The camera isn’t parallel with the surface normal.
Is this because of some inaccuracy when using CFrame.fromRotationBetweenVectors?

It should just be this:

local newCFrame = CFrame.lookAt(self.Instance.CFrame.Position, self.Instance.CFrame.Position + result.Normal)

Could also use the new lookAlong method

That would work if I had a Vector3 for lookAt.

Here’s the solution I found:

local newCFrame = CFrame.fromRotationBetweenVectors(
		self.Instance.CFrame.UpVector,
		result.Normal
	)
	
	newCFrame *= self.Instance.CFrame.Rotation
	newCFrame += result.Position + result.Normal

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