Making a camera transition to a new spot?

local angle = 0
while angle < 90 do
    camera.CoordinateFrame = CFrame.new(target.Position) 
	                           * CFrame.Angles(0, angle, 0) 
	                           * CFrame.new(0, 0, -10)       
	angle = angle + math.rad(1)
	wait()
end

I’m trying to make my camera go from (0, 0, 0) to (0, math.rad(90), 0) (CFrame.Angles) but atm it just keeps spinning. I thought that adding the while angle < 90 would make it stop at 90, but it just keeps going and going and going.

Also another small problem. The camera is set to this when you join:

camera.CoordinateFrame = CFrame.new(target.Position) * CFrame.Angles(0, 0, 0) * CFrame.new(0, 0, 2)

Now it changes the CFrame.new(0, 0, 2) to CFrame.new(0, 0, -10) when you click a button (haven’t included all that because it’s not important. When you click the button it goes instantly from 2 to -10. Is there a way to make it slowly go from Position A to Position B?

This is probably what you’re looking for:

camera:Interpolate(newCFrame, newFocus, duration)

It will move the camera from it’s current CFrame and Focus to a new CFrame and Focus over the given duration.

Probably best to use tweenservice if you want a nice effect on the camera, and more control over the motion.
:Interpolate () works just as well though, if you don’t want to go the the extra effort of that.

1 Like

If you’re interested in learning how interpolation works, google linear interpolation. The formula for it is relatively simple (v0 + (v1 - v0 * t)), but super useful for loads of stuff

Lerping doesn’t work nicely for camera interpolating, for example when the start and end cframe are pointing in almost the completely opposite direction. Slerping is better.

Slerp between two CFrames:

local function slerp(c0, c1, t)

	t = math.clamp(t,0,1)

	-- Lerp position:
	local position = c0.p:Lerp(c1.p, t)
	
	-- Slerp look-vector:
	local omega = math.acos(c0.lookVector:Dot(c1.lookVector))
	local lookVector
	if omega < 0.0001 then
		-- Difference in rotation is negligible:
		lookVector = c0.lookVector
	else
		-- Slerp formula to interpolate uniformly between the two look-vectors:
		lookVector = math.sin((1 - t)*omega)/math.sin(omega)*c0.lookVector + math.sin(t*omega)/math.sin(omega)*c1.lookVector
	end
	
	-- Construct new cframe from these two components:
	return CFrame.new(position, position + lookVector)
	
end
8 Likes