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
Unsure why it keeps going down to these low numbers. It should stay within like -1 to 1 range for x,y,z
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
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.
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
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.