CFrame conditional not being met when tweening back to last CameraType.Custom cframe

I have a camera controller that first transitions to an object in the distance(focusPart), and then returns to where the player’s camera was last at before it switched to view the focusPart. The player has the ability to move around during this transtion, so a part weld to the player is used to keep track of that cframe.

I don’t know why, but the == conditional fails to be met in the loop below. Using something like if math.abs(cam.CFrame.x - savedCamPart.CFrame.x) < 1 and math.abs(cam.CFrame.y - savedCamPart.CFrame.y) < 1 and math.abs(cam.CFrame.z - savedCamPart.CFrame.z) < 1 then also brings error, except the error is a pause between the camera change and tween when being met.

--   ...

local cam = workspace.CurrentCamera

local function tweenCamera(camera, cframe, duration, style)
	local goal = {
		CFrame = cframe,
	}
	local tweenInfo = TweenInfo.new(
		duration,
		style,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	
	local tween = TweenService:Create(camera, tweenInfo, goal)
	tween:Play()
	
	return tween
end

wait(2)
-- last cam position before focusing on other part
originCamCFrame = cam.CFrame

local savedCamPart = Instance.new("Part", player) -- player is the character
savedCamPart.Anchored = false
savedCamPart.CanCollide = false
savedCamPart.Transparency = 0.3
savedCamPart.Massless = true
savedCamPart.CFrame = originCamCFrame

-- part is weld to player and given last cam position to account for player movement during camera focus change
local weld = Instance.new("WeldConstraint", savedCamPart)
weld.Part0 = savedCamPart
weld.Part1 = humroot

cam.CameraType = Enum.CameraType.Scriptable
tweenCamera(cam, focusPart.CFrame, 3, Enum.EasingStyle.Cubic)

wait(2)

connection = RunService.Heartbeat:Connect(function()
	tweenCamera(cam, savedCamPart.CFrame, 2, Enum.EasingStyle.Cubic)
	
    if cam.CFrame == savedCamPart.CFrame then
		cam.CameraType = Enum.CameraType.Custom
		savedCamPart:Destroy()
		connection:Disconnect()
	end
end)

Sorry, your actual explanation of what you’re trying to achieve was unclear; but you cannot compare separate CFrame options like that. Additionally you can’t accurately compare float values. You’ll have to come up with a different solution.

Perhaps you were trying to compare the position instead. You could do something like…

math.abs(cam.CFrame.Position.x - SavedCampart.CFrame.Position.x) < 1

This works, however the switch to CameraType.Custom has a long pause https://streamable.com/fuix0h.
Here’s the updated loop:

connection = RunService.Heartbeat:Connect(function()
    tweenCamera(cam, savedCamPart.CFrame, 2, Enum.EasingStyle.Cubic)

	local xcond = math.abs(cam.CFrame.Position.x - savedCamPart.CFrame.Position.x) < 1
	local ycond = math.abs(cam.CFrame.Position.y - savedCamPart.CFrame.Position.y) < 1
	local zcond = math.abs(cam.CFrame.Position.z - savedCamPart.CFrame.Position.z) < 1
	
	if xcond and ycond and zcond then
		savedCamPart:Destroy()
		cam.CameraType = Enum.CameraType.Custom
		connection:Disconnect()
	end
end)

disconnect from the event before you set the cameraType to custom

That doesn’t seem to affect it, though it’s interesting how disconnecting before all lines have executed doesn’t stop prematurely like a co-routine yield.

Make sure that all tweens have been cancelled before resetting the camera type?

1 Like