I want to make the 2D camera change FOV when the part is touched.
The problem I’m having is that while I’m stepping on the part, the camera keeps shifting from it’s original position to the position I want it to be when touched.
I’ve tried to use an onPartTouched() function but it didn’t do anything.
Video showcasing the problem below
Code:
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(0, 5, 25), hrp.Position), 0.1)
end)
workspace.CameraChangePart.Touched:Connect(function(touched)
local humanoid = touched.Parent:WaitForChild("Humanoid")
if humanoid then
cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 15, 25), hrp.Position), 0.1)
end
end)
The problem is that every time you touch the part, you run the function again, which keeps resetting the camera over and over, resulting in this shaking effect. You can fix this by using a debounce variable, so that the camera only changes the first time that you touch the part.
local cam = workspace.CurrentCamera
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local canAdjustCamera = true
game:GetService("RunService").RenderStepped:Connect(function()
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 5, 25), hrp.Position), 0.1)
end)
workspace.CameraChangePart.Touched:Connect(function(touched)
local humanoid = touched.Parent:WaitForChild("Humanoid")
if humanoid and canAdjustCamera then
canAdjustCamera = false
cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + Vector3.new(0, 15, 25), hrp.Position), 0.1)
end
end)