Hello! I need some help with my third-person toggleView.
The issue is as follows: when I switch to third-person view, the perspective is not displayed correctly, and the player lacks a head and doesn’t move as expected. How can I adjust it correctly when switching to third-person view?
video showing issue:
module script:
-- CameraModuleScript
local CameraModule = {}
function CameraModule.setupCamera(character, camera, forwardOffset, upwardOffset)
local head = character:WaitForChild("Head")
local function updateCameraPosition()
local offset = Vector3.new(0, upwardOffset, forwardOffset)
camera.CFrame = CFrame.new(head.Position + offset)
end
-- Set the initial camera position
updateCameraPosition()
-- Continuously update the camera position to follow the head
head:GetPropertyChangedSignal("Position"):Connect(updateCameraPosition)
local isThirdPerson = false
local function toggleCameraView()
print("toggleCameraView called")
isThirdPerson = not isThirdPerson
if isThirdPerson then
-- Change to third-person view
print("Changed to third-person view")
camera.CameraType = Enum.CameraType.Scriptable
else
-- Change back to first-person view
print("Changed to first-person view")
camera.CameraType = Enum.CameraType.Custom
end
end
local function setupInput()
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
toggleCameraView()
end
end)
end
setupInput()
end
return CameraModule
localscript:
-- LocalScript
local CameraModule = require(game.ReplicatedStorage:WaitForChild("CameraSwitchModule"))
local player = game.Players.LocalPlayer
local function onCharacterAdded(character)
local camera = game.Workspace.CurrentCamera
-- Adjust these variables for customization
local forwardOffset = 3
local upwardOffset = 1.5
-- Use the function from the module to set up the camera
CameraModule.setupCamera(character, camera, forwardOffset, upwardOffset)
end
-- Check if the character is already there
local character = player.Character
if character then
onCharacterAdded(character)
end
-- Listen for character added
player.CharacterAdded:Connect(onCharacterAdded)
project organization: