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
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.
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.