How can I smoothly tween my camera back to the player

I have written a script that smoothly tweens the camera to its desired CFrame when the player enters a part, but it sadly does not smoothly tween back towards the player when it exits. Is there anyway I can make this possible?

3 Likes

Because the camera was tracking a point in the first tween but is now going back to tracking the character, I don’t know if it’d be possible. You can, however, do some GUI work and make a fade-in/out screen to smoothly transition as an alternative.

I mean I made something that kind of worked.

Basically, right when the player entered, I used a placeholder variable to fill in the CurrentCamera’s CFrame. When the player leaves, the camera tweens to that CFrame.


The only problem, however, is that this only looks smooth if the player enters from reasonably the same spot they entered. I just need to know how to make it work 100% of the time.

Script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local camera = game.Workspace.CurrentCamera
local platformerParts = workspace:WaitForChild("Objects"):WaitForChild("CamPartsStationary")

local tweenservice = game:GetService("TweenService")

local first = true
local originalcframe

game:GetService("RunService").RenderStepped:Connect(function()
	local inside, detection = false, nil
	for _, part in ipairs(platformerParts:GetChildren()) do 
	    if part:IsA("BasePart") then
	        local relPos = part.CFrame:PointToObjectSpace(humanoidRootPart.Position)
	        if math.abs(relPos.X) < part.Size.X * 0.5 and math.abs(relPos.Y) < part.Size.Y * 0.5 and math.abs(relPos.Z) < part.Size.Z * 0.5 then
	            inside, detection = true, part
	            break
	        end
	    end
	end
	
	if inside and detection then
		if humanoid.AutoRotate == false then
			humanoid.AutoRotate = true
			wait(0.1)
		end
		if (camera.CFrame.Position - camera.Focus.Position).Magnitude < 2 then
			player.CameraMinZoomDistance = 12
			wait(0.1)
			player.CameraMinZoomDistance = 0.5
		end
		camera.CameraType = Enum.CameraType.Scriptable
		
		if first then
			first = false
			
			originalcframe = camera.CFrame
			
			local tween = tweenservice:Create(
				camera,
				TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
				{
					CFrame = detection.Cam.CFrame,
					Focus = CFrame.new(0,0,100)
				}
			)
			
			tween:Play()
			
			wait(0.6)
		end
	else
		if not first then
			first = true
			
			player.Movements.CanMove.Value = false
			humanoid.WalkSpeed = 0
			humanoid.JumpPower = 0
			
			local tween = tweenservice:Create(
				camera,
				TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.In),
				{
					CFrame = originalcframe,
					Focus = CFrame.new(0,0,100)
				}
			)
			
			tween:Play()
			
			wait(0.6)
			
			player.Movements.CanMove.Value = true
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = 50
			
			camera.CameraType = Enum.CameraType.Custom
			camera.CameraSubject = humanoid
		end
	end
end)
3 Likes

Try saving a CFrame relative to cameras distance from the player. Get the different in positions, and save the LookVector, and then apply that to the returning tween

-- entering part
local camCFrame = Camera.CFrame
local camDistance = camCFrame.p - Player.Character.Head.Position -- may need to switch the order, idk
-- leaving part
local newPos = Player.Character.Head.Position + camDistance
local cameraCFrame = CFrame.new(newPos, newPos + camCFrame.LookVector)
11 Likes

Thank you very much! Works like a charm.

4 Likes

Here’s the final product if you were wondering:

External Media
7 Likes

Hey i know it is kind of late, but can you send me how you made it tween back because i dont find any solution! Thank you and have a good day!

Here I made a post about it and found the solution thanks to @goldenstein64: Camera not working - Help and Feedback / Scripting Support - DevForum | Roblox