Is there a better way to switch through cameras?

Hello!

I hope you’re well. I have 3 Camera LocalScripts meant to be used for different setups. I have a camera script to show the perspective of 2D, another LocalScript so the camera shows the overhead of the user, and the last one is when a person dies.

They work fine! Though, I want to optimize them. I’m not sure how to do that. I tried converting them into a ModuleScript but it wasn’t working very well, assuming because of the way the Cameras are set up. What can I do?

Here’s one of the camera scripts, they all follow the same structure, just different values.

local cam = workspace.CurrentCamera

local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")

game:GetService("RunService").RenderStepped:Connect(function()

	cam.CameraType = Enum.CameraType.Scriptable

	cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(-50, 0, 0), hrp.Position), 0.05)
end)

It’s currently located in StarterCharacterScripts
image

As of now, if a camera needs to switch, it disables the previously active camera and enables the new camera. I don’t think it’s very ideal when it comes to optimization and I really want to improve it.

Thanks,
Aki

1 Like

If you are using this exactly code you have posted to the 3 cameras, one thing you should do is to verify firstly if that camera are in use in that moment, so the rest of the code doesn’t need to process.

Example:

Run code service here: conect...
   if InUse == true then
     process cam.Cframe pos and so on...
   end 

if you dont do that i assume all the 3 cameras are changing the CFrame for NOTHING

POINT 2: You dont need to set Camera Type to Scriptable every frame. so this line needs to be outside the Run Service Render Stepped!

1 Like

Thanks for your suggestions! Though, in my current system, why would I need an InUse variable? (In the context of the current system of course)

Currently, per say if you pass an invisible wall that should change your camera, I made it so it Disables the active camera script and enables the other one that needs to be used. I’m sorry if I wasn’t clear.

I’ll fix that now, thanks for letting me know! I should probably mention that these scripts are quite old and I have made several projects ago from years back. I’m sorry if they seem a bit weird.

Though, do you have a suggestion on how I can make use of only one script (preferably a ModuleScript) and stick with that? I tried putting the code into separate functions, it did change the perspective but not as expected, as it moved the camera elsewhere instead. Thanks :slight_smile:

1 Like