CameraOffset Ignores Collision

When I set camera offset of a humanoid and there’s a part in front, the camera just clips right through the part. Are there any alternatives? I’m looking for away for the part to push the camera back to a vector of 0,0,0 kind of like the third person zoom.

1 Like
local player = game.Players.LocalPlayer

local char = script.Parent
local head = char:WaitForChild("Head")
local humanoid = char:WaitForChild("Humanoid")

local camera = game.Workspace.CurrentCamera

local cameraOffset = Vector3.new(0,0,-3) --- Set this to what you want

humanoid.CameraOffset = cameraOffset

while char ~= nil and humanoid.Health > 0 do
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.FilterDescendantsInstances = {char}
	
	local ray = game.Workspace:Raycast(head.Position, head.CFrame.LookVector * (cameraOffset.Z)*-1)
	
	if ray == nil or ray.Instance == nil then
		humanoid.CameraOffset = cameraOffset
		
		player.CameraMinZoomDistance = 0
		player.CameraMaxZoomDistance = 0
	else
		humanoid.CameraOffset = Vector3.new(0,0,0)
		
		player.CameraMaxZoomDistance = 10
		player.CameraMinZoomDistance = 10
	end
	
	task.wait(0.1)
end

Basically what i did here was shoot a ray infront of the character’s head which length is determined by the z value of the camera offset, if it detects a part in its range it will return to third person.
Hopefully this is what you are looking for.

It’s a first person only game, I don’t want it to move to third person. Like stated in the post, I want it to push to camera back similarly to how the camera system in third person works. I do not want to make the camera third person.

1 Like

haha Sorry for the misunderstanding anyways here it is

local player = game.Players.LocalPlayer

local char = script.Parent
local head = char:WaitForChild("Head")
local humanoid = char:WaitForChild("Humanoid")

local camera = game.Workspace.CurrentCamera

local cameraOffset = Vector3.new(0,0,-3) --- Set this to what you want

humanoid.CameraOffset = cameraOffset

while char ~= nil and humanoid.Health > 0 do
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.FilterDescendantsInstances = {char}
	
	local ray = game.Workspace:Raycast(head.Position, head.CFrame.LookVector * (cameraOffset.Z)*-1)
	
	if ray == nil or ray.Instance == nil then
		humanoid.CameraOffset = cameraOffset
	else
		humanoid.CameraOffset = Vector3.new(cameraOffset.X, cameraOffset.Y, -math.abs(ray.Position.Z - head.Position.Z))
	end
	
	task.wait()
end

basically what its’s doing is setting the z of the cameraOffset to the distance between the object and wall if there is any