Can't seem to set camera back to player

Hey all, quite frankly im ashamed to ask this as it’s such a simple task, however, I can’t seem to do it, despite having done this successfully probably hundreds of times in the past.

Simply, I want to set the camera subject and camera type to what they need to be to focus directly on the player character. I currently change the camera subject and use interpolation for an effect ingame, then run this to move the camera back to default character focus, but when I run both this code;

camera.CameraSubject = plr.Character:FindFirstChildOfClass("Humanoid")

camera.CameraType = Enum.CameraType.Custom

and this code

camera.CameraSubject = plr.Character:FindFirstChildOfClass("Humanoid")
camera.CameraType = Enum.CameraType.Follow

I end up with a black screen. I’ve tried repeating the code at intervals, changing it’s order, and a lot more but I just can’t seem to get it working. Here’s a useless picture of what the screen looks like:

3 Likes

Have you tried changing camera.CameraSubject to just player.Character, rather than .Humanoid? (With CameraType.Custom)

It still doesn’t work, I realise this is very likely a problem with the surrounding code, so here it is just in case I’m being an idiot and not seeing something obvious:

close.MouseButton1Click:Connect(function()
	if windowOpen then
		windowOpen = false
		viewQuestions:TweenPosition(UDim2.new(1, 0, 1.3, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.4)
		rightBar:TweenPosition(UDim2.new(1, 0, 0, 0),Enum.EasingDirection.In, Enum.EasingStyle.Quad, 0.4)
		pcall(function() camera:Interpolate(plr.Character.Head.CFrame, plr.Character.Head.CFrame, 0.6) end)
		spawn(removeBlur)
		wait(0.7)
		mainUI.Enabled = true
		camera.CameraSubject = plr.Character
		camera.CameraType = Enum.CameraType.Custom
	end
end)

Try

camera.CameraSubject = plr.Character.Humanoid

1 Like

:FindFirstChildOfClass("Humanoid") is the same thing, and I frequently change the name of the player’s humanoid for scripts in the game. I gave it a try anyway and no luck :frowning:

game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"

Always works for me so I don’t think the code you posted is the issue. Are you sure another script isn’t stopping it being set or immediately changing it back?

7 Likes

I was only reading your second code… Oops.

Maybe try changing the order? I always set the camera type first.

camera.CameraType = Enum.CameraType.Custom
camera.CameraSubject = plr.Character:FindFirstChildOfClass("Humanoid")

Otherwise I have no idea. You must be messing with the camera somewhere else.

2 Likes

Pretty sure you set it to the HumanoidRootPart and not the Humanoid

If you set it to HRP then you get a camera shake and can zoom the camera through can collide true parts

2 Likes

I just tried this and it still doesn’t work.

I also tried setting the camerasubject and cameratype to something different then setting it back in a blank baseplate and it worked. Either it’s something to do with Interpolate or I’m unknowingly manipulating the camera elsewhere.

1 Like

I’ve just tried testing this in a blank baseplate;
first set the camera subject and camera type:

game.Workspace.CurrentCamera.CameraSubject = game.Workspace.Baseplate
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable

Now run this code:

local camera = game.Workspace.CurrentCamera
local plr = game.Players:FindFirstChildOfClass("Player")

pcall(function() camera:Interpolate(plr.Character.Head.CFrame, plr.Character.Head.CFrame, 0.6) end)
local deletDis
deletDis = camera.InterpolationFinished:Connect(function()
	print("Putting camera back.")
	camera.CameraSubject = plr.Character.Humanoid
	camera.CameraType = Enum.CameraType.Follow
	deletDis:disconnect()
end)

There’s something wrong with the code, not other scripts manipulating the camera. :confused:

1 Like

The default camera type is Custom, not Follow. Does that solve the issue?

I’m able to accomplish this simply by storing the CurrentCamera CFrame and Focus in a variable before tweening/interpolating the camera. When I need to tween/interpolate back to the Player I just use the values from the Variables I stored them in, so like this:

(Here’s a .rbxl for ease of access, just hit Play and test it out) CamTween.rbxl (16.9 KB)

https://gyazo.com/3672d2b3f08e213666de6f002e6dff26

local tweenService = game:GetService('TweenService')

local currentCamera = workspace.CurrentCamera
local isViewing = false
local orginCameraCFrame = nil
local originCameraFocus = nil --IF USING currentCamera:Interpolate
local partToTweenTo = game.Workspace.partToTweenTo

script.Parent.MouseButton1Click:connect(function()
	if not isViewing then
		isViewing = true

        currentCamera.CameraType = Enum.CameraType.Scriptable
        orginCameraCFrame = currentCamera.CFrame
        originCameraFocus = currentCamera.Focus --IF USING currentCamera:Interpolate
      
        local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Quint, Enum.EasingDirection.In, 0)
        local tweenGoal = {CFrame = partToTweenTo.CFrame}
        local tweenAnim = tweenService:Create(currentCamera, tweenInfo, tweenGoal)
        tweenAnim:Play()
    elseif isViewing then
		isViewing = false

        currentCamera.CameraType = Enum.CameraType.Scriptable

        local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Quint, Enum.EasingDirection.In, 0)
        local tweenGoal = {CFrame = orginCameraCFrame}
        local tweenBackAnim = tweenService:Create(currentCamera, tweenInfo, tweenGoal)
        tweenBackAnim:Play()

		tweenBackAnim.Completed:Connect(function(playbackState)
			currentCamera.CameraType = Enum.CameraType.Custom
		end)
    end
end)

If you don’t want to bother re-tweening back to the player at the end, just remove all of the tween bit’s as well as the currentCamera.CameraType = Enum.CameraType.Scriptable, and just have currentCamera.CameraType = Enum.CameraType.Custom.

If you do decide to keep the tween at the end I suggest either anchoring the Player’s torso or preventing them from moving, as if they move they are at a different position than the one saved in the variable.

You can avoid this completely by just doing

isViewing = false
currentCamera.CameraType = Enum.CameraType.Custom

this cuts straight back to the Player’s original camera

17 Likes

Hey! I tried your script but it isn’t seem to be working? It just set it to the origin when I click back. Here’s a video and my script:

local camera = workspace.CurrentCamera -- our camera
local player = game.Players.LocalPlayer -- our player
local characterCreation = script.Parent.Parent.Parent
local TS = game:GetService("TweenService")
local orginCameraCFrame = nil
local originCameraFocus = nil
local isViewing = true

local tweenInfo = TweenInfo.new(
	1, -- Time
	Enum.EasingStyle.Quart, -- EasingStyle
	Enum.EasingDirection.Out -- EasingDirection
)

orginCameraCFrame = camera.CFrame
originCameraFocus = camera.Focus

script.Parent.MouseButton1Click:Connect(function()
	if characterCreation.Enabled == true and isViewing == true then -- if the editor frame is already visible
		
		
		local findEditorFolder = workspace:WaitForChild("EditorFolder")
		--local findEditorFolder = workspace:WaitForChild(player.Name.."EditorFolder") -- we look for the folder where all our camera stuff is inside of it (in workspace)
		if findEditorFolder then -- if it was found then..
			characterCreation.Enabled = false and isViewing == false
			camera.CameraType = "Scriptable" -- we actually just want to change the camera back to normal, we do that by changing the camera type to custom
			camera.CameraSubject = workspace:WaitForChild(player.Name).Humanoid -- the subject to the player's humanoid
			--local tweenOut = TS:Create(camera, tweenInfo, {CFrame = workspace:WaitForChild(player.Name).Head.CFrame})
			local tweenOut = TS:Create(camera, tweenInfo, {CFrame = orginCameraCFrame})
			tweenOut:Play()
			tweenOut.Completed:Connect(function(playbackState)
				camera.CameraType = Enum.CameraType.Custom
			end)
		end
		script.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 16
		script.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.JumpPower = 50
		
	end
end)

Video

It appears that the solved solution broke after interpolation was deprecated. To save the original position and orientation of the camera I sorta created a janky method that feels caveman like but it gets the job done. What I did was:

local cameraPart = Instance.new(Part)
cameraPart.Size = Vector3.new(.5,.5,.5)
cameraPart.Transparency = 0
cameraPart.Anchored = true
cameraPart.CFrame = game.Workspace.CurrentCamera.CFrame
cameraPart.Parent = game.Workspace

(I basically created a new part with the same CFrame as the current camera so I can have the camera tween to the new part’s cframe, and after the cutscene is done I just delete the part, the transparency is 0 just so I can see what is going on but in game it should be set to 1)