Camera changing help

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)

What’s the problem?

Your RenderStepped loop is running at the same time as you are setting the new CFrame. To prevent this, just add a controllable variable.

Code:

local cam = workspace.CurrentCamera

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

local cameraOffset = Vector3.new(0, 5, 25)

game:GetService("RunService").RenderStepped:Connect(function()
	cam.CameraType = Enum.CameraType.Scriptable
	
	cam.CFrame = cam.CFrame:Lerp(CFrame.new(hrp.Position + cameraOffset, hrp.Position), 0.1)
end)

workspace.CameraChangePart.Touched:Connect(function(touched)
	local humanoid = touched.Parent:WaitForChild("Humanoid")
	if humanoid then
		cameraOffset = Vector3.new(0, 15, 25)
	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)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.