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.