Hello, everyone. I’m currently editing a home screen system for my game and I’ve a struggle with the camera while tweening.
When the player clicks on “PLAY”, the camera’s CFrame has to tween until reaching the character’s position of the player (which can vary). Then the camera’s property CameraType is set to Custom so the player can control it.
As you can see bellow, the script doesn’t work correctly because the transition only remains smooth for a time, and then does like a “cut” because the CFrame the camera reaches isn’t correctly set. And I don’t understand why.
Here’s my init script:
-- When the game runs
local TweenService = game:GetService("TweenService")
local current_cam = game:GetService("Workspace").CurrentCamera
local position = current_cam.CFrame
-- Then the current_cam.CameraType is set as Scriptable so there's a cinematic (looping) as you can see in the video above
And this is what happens when the “PLAY” button is pressed:
It’s been months and months I try to understand how to reset player’s camera behind the character but it never works. I’ve seached for so long on the DevForum but even though it doesn’t work.
Anyways, thanks a lot for your future help. I’d be greatful for it.
Have a nice day, everybody!
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local currentCam = Workspace.CurrentCamera
local player = Players.LocalPlayer
local playButton = script.Parent -- if this script is a child of PLAY
local function onPlayClicked()
local charCamValue = playButton.Parent.Values.Char_cam.Value
local camTween = TweenService:Create(currentCam,
TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut),
{CFrame = charCamValue}
)
camTween:Play()
camTween.Completed:Connect(function()
currentCam.CameraType = Enum.CameraType.Custom
end)
end
playButton.MouseButton1Click:Connect(onPlayClicked)
First and foremost, I’d like to thank you for your time.
I’ve taken a look at your script and it seems to be exactly what I’ve done in mine.
Of course, in the topic I didn’t send all my script cuz I wanted you to focus only on the important parts. My script is build on different LocalScripts, so I only wanted the main elements to be shown here.
I guess your script won’t solve my problem in his current version however.
I fixed this problem a long time ago but I forgot how.
Where does script.Parent.Parent.Values.Char_cam.Value come from?
I vaguely remember needing to tween to a cframe that is aimed at the camera subject position (I think HRP’s position but up some offset), then needing to calculate the camera distance from the subject position (possibly adding or subtracting 0.5 for the clipping/min-zoom distance), then setting the min and max camera distance to both be the calculated distance, then setting them back.
I think I remember that this needs to be done because the custom camera controller internally stores the zoom distance, so even though it uses the camera’s orientation when swapped it doesn’t take the zoom from the camera. Setting the min and max zoom to be the same forces the controller to have that value.
You might need to wait a frame or two before setting the min and max camera zoom back. When testing I would start with a long wait (task.wait(0.1)) to see if it works then try no wait (see if it breaks) and if it does break a task.wait() or two.
Okay, writing this has jogged my memory. This was the solution I used.
I think first you should change script.Parent.Parent.Values.Char_cam.Value to be something that aligns with the character (and even better the camera subject). For some reason the current value doesn’t seem to, though it might just be the video.
You’ll notice in the video the camera’s zoom also locks. That’s the thing you need to use Player.CameraMinZoomDistance and Player.CameraMaxZoomDistance to “set” inside the custom camera controller.
Here is some example code:
local roughCFrame -- set this to your old goal, the code will use the orientation from it
local humanoidRootPart -- set this to the HRP
local CAMERA_SUBJECT_OFFSET = Vector3.new(0, 1.5, 0) -- This value might need to be changed around a bit
local cameraSubjectPosition = humanoidRootPart.Position + CAMERA_SUBJECT_OFFSET
local cameraZoom = 8 -- (Constant value)
--local cameraZoom = (cameraSubjectPosition - roughCFrame.Position).Magnitude -- An alternative you might like better if a constant value works
local CAMERA_ZOOM_OFFSET = -0.5 -- For a constant value this won't matter but for the second option this might need to be 0, 0.5, or -0.5
-- Make a cframe with the correct orientation and position at the subject
local goalCFrame = roughCFrame - roughCFrame.Position + cameraSubjectPosition
-- Move it back the zoom amount
goalCFrame -= goalCFrame.LookVector * (-1 * cameraZoom)
local tween -- Creator your tween with the goalCFrame
local Players = game:GetService("Players") -- move to top of code
tween.Completed:Once(function()
local oldMin = Players.CameraMinZoomDistance
local oldMax = Players.CameraMaxZoomDistance
Players.CameraMinZoomDistance = cameraZoom + CAMERA_ZOOM_OFFSET
Players.CameraMaxZoomDistance = cameraZoom + CAMERA_ZOOM_OFFSET
-- Change the camera type back to custom here
task.wait(0.1) -- change out for no wait, task.wait(), or maybe two task.wait()'s
Players.CameraMinZoomDistance = oldMin
Players.CameraMaxZoomDistance = oldMax
end)
Thanks a lot for your time!
I’m very greatful of what you’ve done in order to help me
However, I’ve just set the problem: in fact, I was doing my tests moving the character. However, when the cutscene started, I was still maintaining “Z” on my keyboard for less than a second (time needed by the human brain to realize what’s happening). However, the position of the camera was saved, but wrongly because I moved a bit while it was already set. That’s why the bug occured: the camera tweened in the good position but the player had moved, so when I changed the camera.CameraType to Custom, it would obviously move.
Nevertheless, I’m really greatful for both of your answers and of your time, guys. Thanks a lot.