I’m currently facing challenges in achieving a smooth camera rotation while trying to look towards a point.
In my code, I’m using the concept of angular velocity to control the rotation of the camera. I want the camera to smoothly rotate towards a position, similar to how objects move in a velocity-based system. However, I’m encountering weird issues and bugs. It’ll do random jerk or slow motions. I’m not super good at math, but I’ve done some research. I don’t want to use linear interpolation, as this would cause stutter(have tested).
The position part of the code works, and has no errors/problems. I’ve tested extensively, and I decided to just start on the rotation part of the bit. I wouldn’t be surprised if my rotation code is completely wrong.
Code Snippet:
--[[Position]]--
local direction = (newCFrame.Position - targetCFrame.Position).Unit
local distance = (newCFrame.Position - targetCFrame.Position).Magnitude
local velocity = direction * math.min(distance*velocityMultiplier, maxSpeed*dt)
local v1 = targetCFrame + velocity * dt;
if not hasNaN(v1) then
targetCFrame = v1
end
--[[Rotation Based On Position]]--
local currentDirection = targetCFrame.LookVector.Unit
local futureDirection = (futurePosition - targetCFrame.Position).Unit -- future position determines what point to look at next
local angle = math.acos(currentDirection:Dot(futureDirection));
local angularVelocity = angle * math.min(1,maxAngularSpeed * dt);
local axis = currentDirection:Cross(futureDirection).Unit
local rotationIncrement = CFrame.fromAxisAngle(axis, angularVelocity);
local v2 = targetCFrame * rotationIncrement
if not hasNaN(v2) then
targetCFrame = v2
end