Custom Camera Collisions

I created a custom character and was looking if anyone could help me create collisions for my camera.
This is the module script that controls the camera and I will show the RenderStepped function below:

function Computer:StartCamera(Camera,Character,Mouse)
	local RootPart
	
	CameraConnection = RunService.RenderStepped:Connect(function()
		
		RootPart = Character.HumanoidRootPart
		
		local CameraPosition = Camera.CFrame.Position
		
		local Scale = 300
		local Center = Vector2.new(Camera.ViewportSize.X/2, Camera.ViewportSize.Y/2)
		local NewMoveVector = Vector3.new((Mouse.X-Center.X)/Scale,-(Mouse.Y-Center.Y)*3/Scale,0)
		
		if isHoldingRightClick then
			MoveVector = NewMoveVector
		else
			MoveVector = Vector3.new(0,0,0)
		end
		
		BackCameraRay = Ray.new(CameraPosition,-Camera.CFrame.LookVector * 5)
		local HitPart,HitPos,Normal = workspace:FindPartOnRay(BackCameraRay,Character,false,true)
		
		if HitPart then
			print(HitPart)
		end
		
		CameraTween = TweenService:Create(Camera,CameraTweenInfo,{CFrame = CFrame.new((RootPart.Position + RootOffset),RootPart.Position + Offset + MoveVector)})
		CameraTween:Play()
		
	end)
	
end

https://gyazo.com/35e02cf9746902c1c815a4557aa43b8a
The camera works fine, but how can I add collisions to it?


you could fire a raycast, starting at the cameras lookVector
if it hits any parts that are ‘close’ then it can zoom to the player until it no longer hits a ‘close’ part


I would look at these two references in the meantime.


I’ve never really attempted custom camera collision, or I would offer code.
hopefully someone gives you a full answer soon.


1 Like

This isn’t something I personally have done before, but I have thought about how I would go about doing it if I had to.
Create a ray with it’s origin located at the focus of your camera, which in this case would be your player. The direction you provide to the ray should be your intended camera position subtracted by the ray’s origin point.
Then, pass the ray onto the function FindPartOnRay. The second argument to the function should be the local player’s character so the ray won’t return any intersections with the player itself. If there is a part in the way of the camera, the function will return the part and the point where it intersected with the part. You can move the camera to that intersection point, maybe offset slightly towards the player to avoid clipping into the part with the camera.
If the function returns nil for the part, there is no intersection and you can move the camera to its intended position.

You could put the goal CFrame into a variable and set its position as the hit position if a part is found in your back ray calculation.

local CframeGoal = CFrame.new( ( RootPart.Position + RootOffset ), RootPart.Position + Offset + MoveVector )
local BackCameraRay = Ray.new( CframeGoal.Position, -CframeGoal.LookVector * 5 )
local HitPart, HitPos, Normal = workspace:FindPartOnRay( BackCameraRay, Character, false, true )

if HitPart then
    CframeGoal = CFrame.new( HitPos, RootPart.Position + Offset + MoveVector )
end
		
CameraTween = TweenService:Create( Camera, CameraTweenInfo, { CFrame = CframeGoal } )
CameraTween:Play()