Slerp function causing camera to stop working after a period of time

I got this slerp function somewhere on the forums, and it works for its case, but after a while, just stops working and thus breaking my camera, and I get nan returned.

function CameraController:Slerp(v0, v1, t0)
	local Unit
	if t0 == 0 then
		Unit = v0.Unit
	elseif t0 == 1 then
		Unit = v1.Unit
	elseif v0.Unit == v1.Unit then
		Unit = v0.Unit
	else
		local Cross = v0.Unit:Cross(v1.Unit)
		local t1 = math.asin(Cross.Magnitude) * t0
		Unit = CFrame.fromAxisAngle(Cross.Unit, t1) * v0.Unit
	end
	
	return Unit
end

--// Update camera movement (BindToRenderStep)
function CameraController:Update(deltaTime)	
	if self.LastTouch then
		self.CameraLookVector = self:Slerp(
			self.CameraLookVector,
			self.LastTouch.WorldCFrame.LookVector,
			2 * deltaTime
		)
	end

-- self.CameraLookVector returns nan

Like I said, this works fine, but after about a minute of playing it just starts returning nan for no reason. No errors. It also only goes to the else statement the whole time. I’m not good with 3d math/vectors/etc. so unsure what any of it actually means, but ye

self.CameraLookVector seems to get smaller and smaller until nan
image
Unsure why it keeps going down to these low numbers. It should stay within like -1 to 1 range for x,y,z

I might be missing something, but I don’t see where you assign a value to self.CameraLookVector.

Since I can’t tell what the value is set to it’s hard to find an issue.

I assign self.CameraLookVector here

My bad, I misread.

Are you able to find any issues regarding the self:Slerp() by any chance? Since it is constantly decreasing, and you are basing the value of off itself, there may be an issue with your goal value and/or calculation?

Unsure, I dont know how slerp is actually working. But it shouldn’t be going down to these absurd numbers. I try clamping the numbers, but it’s not really perfect

self.CameraLookVector = Vector3.new(
		math.clamp(math.floor(self.CameraLookVector.X * 10000) / 10000, -1, 1),
		math.clamp(math.floor(self.CameraLookVector.Y * 10000) / 10000, -1, 1),
		math.clamp(math.floor(self.CameraLookVector.Z * 10000) / 10000, -1, 1)
	)
	print(self.CameraLookVector, "|", self.LastTouch.WorldCFrame.LookVector)

I need to all be smooth, and come to a stop at a number thats within reason
image

1 Like

I don’t know what the issue is. Sorry.

Best of luck though!

I’d recommend looking into the self:Slerp or slerp overall. Check if everything gives you reasonable numbers. If one function doesn’t provide that, search within it for the cause of the issue.

1 Like

Is there a particular reason you’re using a custom slerp implementation? AFAIK, the Lerp method of CFrames already uses slerping for the rotational component.

So, the result would be something like this:

function Slerp(cfa, cfb, delta)
    return (cfa-cfa.Position):Lerp(cfb-cfb.Position, delta).LookVector
end
2 Likes

Trying to normalize a vector (.Unit) that has a magnitude of 0 returns (nan, nan, nan), because normalizing is done by dividing a vector by its magnitude, and anything divided by 0 isn’t possible, so make sure the vectors don’t have a magnitude of 0 before attempting to normalize them.

I’m unsure how to actually normalize stuff though?

Usually my approach would be something like this:

function NormalizeVector(vector)
    return vector.Magnitude ~= 0 and vector.Unit or Vector3.new()
end