How do I make it so that the players camera focus's on a part?

I want to make it so that the player’s camera focus’s on a part, while also having the player’s character in view.

How should I go about doing this?


1 Like

I have no idea if this could work but it’s worth a try.

You could try using Raycast and cast a ray from the camera towards the lookvector of the camera.

local rayOrigin = camera.CFrame.Position
local rayDirection = camera.CFrame.Position + camera.CFrame.LookVector * CAMERA_RANGE -- X studs infront of the camera

local raycastResult = workspace:Raycast(rayOrigin, rayDirection)

and then just do some basic checks for example if the object isn’t too large and stuff.

if raycastResult then
      if raycastResult.Instance:IsA("BasePart") then
            if raycastResult.Instance.Size.Magnitude < MAX_SIZE then
                  -- yay, raycastResult.Instance is the part that has to be focused on
            end
      end
end

Try this out, might work or might not work.

bump

Just shift the CurrentCamera into a part. If you want it to be smooth, use tweening. Hes an example of what that code might look like:

local part = game.Workspace.Part -- Replace "Part" with the name of the part you want to focus on
local camera = game.Workspace.CurrentCamera

camera.CameraSubject = part
camera.CFrame = part.CFrame

1 Like

Um…

local part = Instance.new("Part", workspace) -- Replace "Part" with the name of the part you want to focus on
part.Anchored = true
local camera = game.Workspace.CurrentCamera

camera.CameraSubject = part

game:GetService("RunService").Heartbeat:Connect(function()
	camera.CFrame = part.CFrame
	
	local info = TweenInfo.new(0.001, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
	
	local tween = game:GetService("TweenService"):Create(part, TweenInfo, {CFrame = script.Parent:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(2, 4, 2)})
	
	tween:Play()
end)
local part = Instance.new("Part", workspace) -- Replace "Part" with the name of the part you want to focus on
part.Anchored = true
local camera = game.Workspace.CurrentCamera

camera.CameraSubject = part

game:GetService("RunService").Heartbeat:Connect(function()
	camera.CFrame = part.CFrame
	
	local info = TweenInfo.new(0.001, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
	
	local tween = game:GetService("TweenService"):Create(part, info, {CFrame = script.Parent:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(2, 4, 2)})
	-- you put `TweenInfo` instead of `info`
	tween:Play()
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.