Custom Camera Raycast Not Working Properly

Hey. I’ve been working on my Custom Camera System, but I ran into a minor problem. I create a Ray Cast from the HumanoidRootPart of the player’s character towards the Camera’s CFrame to detect if there is any object to block a through-wall camera basically.
Though the script doesn’t work properly at all.
Video of the issue:

Code:

local players = (game:GetService("Players"));
local localPlayer = (players.LocalPlayer);
local character = (localPlayer.Character or localPlayer.CharacterAdded:Wait());
local camera = {}
camera.__index = camera
function camera.new()
	local self = {
		origin = (character:WaitForChild("HumanoidRootPart"));
		camera = (workspace.CurrentCamera);
		zoomInfluence = (15 / 7);
		zoom = (-15)};
	return setmetatable(self, camera)
end;

-- // I have cut all the functions except the related ones.
function camera:CreateRayFromOrigin(cframe)
	local rayCastParams = RaycastParams.new();
	rayCastParams.FilterDescendantsInstances = {self.origin.Parent}
	rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	return (workspace:Raycast(self.origin.CFrame.Position, cframe.Position, rayCastParams))
end;

function camera:CalculateCameraCFrame()
	local cframe = (CFrame.new(self.origin.CFrame.Position) * (self.camera.CFrame - self.camera.CFrame.Position) * CFrame.new(0, 0, -self.zoom));
	local rayCastResult = (self:CreateRayFromOrigin(cframe));
	if not (rayCastResult) or (rayCastResult) and (rayCastResult.Instance:IsDescendantOf(character)) then
		return (CFrame.lookAt(cframe.Position, self.origin.CFrame.Position));
	elseif (rayCastResult) and not (rayCastResult.Instance:IsDescendantOf(character)) then
		return (CFrame.lookAt(rayCastResult.Position, self.origin.CFrame.Position));
	end
end;

Help from anybody would be appreciated.

The vimeo link does not work. Can you try to explain it in text?

I have just noticed it, and updated the link. It should now view properly.

1 Like

This looks a bit strange.

This looks unnecessary, since the character is blacklisted in rayCastParams.

I think you can just simplify the if statement to:

if rayCastResult then
    // Found
else
    // Not found
end

I am not sure what causes the glitches to happen. Do you update the camera with RunService:BindToRenderstep(UPDATE_NAME, Enum.RenderPriority.Camera.Value - 1, updateFunction)?

No, a simple .RenderStepped connection.
And I changed this if statement to what you have mentioned, the issue still remains.

‘cframe.Position’ is not a direction (see WorldRoot | Roblox Creator Documentation). I think that that is the issue.

1 Like

I am testing the solution right now, your comment also makes sense.

Thank you very much, the solution worked!

1 Like