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)