#how to make third person camera collide with objects

1 messages · Page 1 of 1 (latest)

winter plover
#

found a useful third person camera script but the camera doesn't collide or get blocked of by walls is there a way to edit the script to do so

local cameraOffset = Vector3.new(2.5, 2, 10) 

player.CharacterAdded:Connect(function(character)

    local humanoid = character:WaitForChild("Humanoid")
    local rootPart = character:WaitForChild("HumanoidRootPart")

    local cameraAngleX = 0
    local cameraAngleY = 0

    humanoid.AutoRotate = false

    local function playerInput(actionName, inputState, inputObject)
        if inputState == Enum.UserInputState.Change then
            cameraAngleX -= inputObject.Delta.X
            cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
        end
    end
    ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

    RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
        local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
        local cameraCFrame =  startCFrame:PointToWorldSpace(cameraOffset)
        local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -100000))

        camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)

        local lookingCFrame = CFrame.lookAt(rootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -100000)))

        rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)

    end)
#

so the goal is to to make the camera be pushed foward/collide with objects then go back to normal

#

how the camera normally works