Problems with Camera raycasts

hi,

ive been trying to make a camera system kind of similar to resident evil’s camera it did go smoothly so far but i have ran into an issue to fix camera clipping,

mostly the problem kind of stems from the way i let the user rotate the camera (poor job of replicating roblox’s way but it gets the job done ig)

so the basic idea of my system is just bringing the camera’s cframe to the root’s and applying a rotation based on the mousedelta’s x and y vectors –

–and then after thats done its gonna set a ZOffset which will give it some sort of “zoom”~ effect
and calculating the ZOffset is the thing im having a problem with right now

and the issue is that the raycast doesnt really seem to function right i mean the function for it is clearly normal but just the origin and direction always have problems sometimes when you smash the camera into a part it doesnt detect it but when you are far from one it detects something

reminder: this uses a :BindToRenderStep() method with the renderpriority being the camera’s or what not

some code samples:

	local _camRotCF = CFrame.fromEulerAnglesYXZ( -- this is the cframe for the camera's rotation
		-math.rad(YAngle),
		-math.rad(XAngle),
		0
	)

	local _cameraCF = _swayCF -- this is the final cframe (ignore _swayCF it doesnt matter here)
		
		* CFrame.new(_hrp.Position)
		* CFrame.new(0, CameraSettings.Y_Offset, 0)
		
		* _camRotCF
		
	--- Set Camera CFrame ---
		
	self.Camera.CFrame = _cameraCF
		
	--- Z Offset --- FOCUS HERE!!!!

	ZOffset = CameraUtil.RayCast(_hrp.Position, _cameraCF.Position - _hrp.Position, {self.Body}) or CameraSettings.Z_MaxOffset

    -- limits so it doesnt break
	if ZOffset > CameraSettings.Z_MaxOffset then
		ZOffset = CameraSettings.Z_MaxOffset
	elseif ZOffset < CameraSettings.Z_MinOffset then
		ZOffset = CameraSettings.Z_MinOffset
	end
		
	--- Set Camera Z Offset ---
		
	self.Camera.CFrame = self.Camera.CFrame * CFrame.new(0, 0, ZOffset) -- applying the zoffset

here is the raycast function

function CameraUtil.RayCast(_origin: Vector3, _direction: Vector3, _exclude: {any}): number?
	if not _origin or not _direction then
		return nil
	end
	
	local _rcp = RaycastParams.new()
	
	if _exclude then
		_rcp.FilterType = Enum.RaycastFilterType.Exclude
		
		_rcp.FilterDescendantsInstances = _exclude
	end
	
	local _result = workspace:Raycast(_origin, _direction, _rcp)
	
	if _result then
		print("hit")
		
		local _dis = (_origin - (_result.Position)).Magnitude
		
		return _dis
	end
end

thank you in advance.

I have no idea what it’s supposed to look like but I will assume the issue is because of how you’re handling the zooming, Camera.CFrame * CFrame.new(0, 0, ZOffset) this would make your camera go towards the direction you’re looking at, by the same distance that the collision was found at.

You don’t necessarily need to multiply cframes when handling this type of offsetting.
Since you know the camera should zoom in at (_hrp.Position - _cameraCF.Position).Unit direction, and that your camera should be at max, CameraSettings.Y_Offset away. Given the returned distance from the raycast, you’d only move your camera by the difference of what distance you want and the distance the raycast gave you:
self.Camera.CFrame += (_hrp.Position - _cameraCF.Position).Unit * (CameraSettings.Y_Offset - result_from_raycast)
Simply, just moving the camera in the direction of where your root part is, instead of where your camera is looking.

Outside of the z-offsetting, regarding raycasts detecting things, you can use RaycastParams.new().BruteForceAllSlow and RaycastParams.new().RespectCanCollide, not exactly sure if that would help the detection part.

sorry if i made thing seem complicated in the original post i wrote it very late but,

the main issue is here (i did edit it a little, _finalCF is just _cameraCF but with a different name)

ZOffset = CameraUtil.RayCast(
	Vector3.new(_finalCF),
			
	Vector3.new(_finalCF * CFrame.new(0, 0, CameraSettings.Z_Offset_Max)),
			
	{self.Body}
) or CameraSettings.Z_Offset_Max

im trying to cast a ray between the _cameraCF (first argument) to the same cf but given the maximum z offset in order to find out whats the maximum it can go without colliding into an object,

i personally think the problem is with the second argument where im trying to predict where the camera’s maximum zoom is,

and in here i just have the same cframe but add the z offset

self.Camera.CFrame = self.Camera.CFrame * CFrame.new(0, 0, ZOffset)

if you are still confused about my messy script i can elaborate more.