Camera will find a CFrame where the player is visible

Camera will find a CFrame where the player is visible.

i Want the camera to look at the player when dead but the player’s camera when alive is in first person, which is i alr succeded but the problem is sometimes its glitching out the wall and the player is not visible.

Here’s the video:

Useless code:

workspace.CurrentCamera.CFrame = CFrame.lookAt(player.Character.PrimaryPart.Position + Vector3.new(3, 9 ,7), player.Character.PrimaryPart.Position)

Use raycast between the character and the new camera position, if a part was in between then change the position to the position of the ray.

how do i get the position of the ray?
ddsdsdsds

If ray then
    Position = ray.Position
end

Honestly, i’ve tried that :slight_smile:
Unfortunately it was giving random parts from my map that i built.

The code:

workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
			local vector = (workspace.CurrentCamera.CFrame.Position - player.Character.PrimaryPart.Position)
			local direction = vector.Unit * vector.Magnitude
			workspace.CurrentCamera.CFrame = CFrame.lookAt(player.Character.PrimaryPart.Position + Vector3.new(3, 9 ,7), player.Character.PrimaryPart.Position)
			local part = workspace:Raycast(workspace.CurrentCamera.CFrame.Position, direction)	
			if part then
				print(part.Instance:GetFullName())
			end

Have you check if the raycast’s direction is correct?

I’m not sure, but if its giving random parts instead of my character that’s definitely not correct.

Try this:

local cameraCFrame = currentCFrame.CFrame:PointToWorldSpace(Vector3.new(3, 9 ,7)) -- Vector3 is the offset not sure if this is the correct of set you want.
local cameraFocus = Character:GetPivot():PointToWorldSpace()

local part = workspace:Raycast(cameraFocus, cameraCFrame - cameraFocus)

if part then
    ...
end

what should i do inside the

if part then

end

cameraCFrame = part.Position