Problem with camera switching

Hi,

I’m trying to make a toggable camera button which like this

  1. User click the button
  2. Camera switchs to part
  3. After clicking again camera switches to humanoid

But I am not able to do the third thing, I’ve tried directly reffering to humanoid instead of using “originalCameraSubject” but still don’t work.

local player = game.Players.LocalPlayer
local cameraPart = workspace.CameraPart
local imageButton = script.Parent
local defaultCamera = game.Workspace.CurrentCamera
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")

-- Initial state of camera mode
local isMapCam = false

-- Store the original camera subject and CFrame
local originalCameraSubject = defaultCamera.CameraSubject
local originalCameraCFrame = defaultCamera.CFrame

-- Function to toggle between camera modes
local function toggleCameraMode()
	isMapCam = not isMapCam

	if isMapCam then
		defaultCamera.CameraSubject = cameraPart
		imageButton.Image = "rbxassetid://14461028872"
	else
		defaultCamera.CameraSubject = originalCameraSubject
		imageButton.Image = "rbxassetid://14461027320"
	end
end

-- Function to update camera position while in map camera mode
local function updateMapCameraPosition()
	if isMapCam then
		local partPosition = cameraPart.Position
		local partLeftVector = cameraPart.CFrame:vectorToWorldSpace(Vector3.new(-1, 0, 0)) -- Calculate the left vector of the part
		local cameraDistance = 10 -- Adjust this distance as needed
		local targetPosition = partPosition + partLeftVector * cameraDistance
		local cameraLookVector = (partPosition - targetPosition).unit
		local cameraCFrame = CFrame.new(targetPosition, targetPosition + cameraLookVector)
		defaultCamera.CFrame = cameraCFrame
	end
end

-- Connect the functions to the MouseButton1Click and Heartbeat events
imageButton.MouseButton1Click:Connect(toggleCameraMode)
game:GetService("RunService").Heartbeat:Connect(updateMapCameraPosition)

Video for reference (Sorry for laggy recording, roblox’s screen recording is bad):

Thanks for help in advance!

3 Likes

When changing the camera’s CameraSubject also try changing the CameraType.

Example:

-- Inside of function to switch camera to the part:
defaultCamera.CameraType = Enum.CameraType.Scriptable

-- Inside of function to switch camera to the player:
defaultCamera.CameraType = Enum.CameraType.Custom

if you add that it should work. Let me know if it doesn’t.

2 Likes

Nothing have changed except not player cannot move the camera when toggled in part. Which toggled back the camera becomes free to move but still not attached back to humanoid.

4 Likes

Fixed it. Instead of

it should be

defaultCamera.CameraSubject = character -- not humanoid
2 Likes

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