Camera focus on a part?

(Sorry if this is in the wrong category, I think it isn’t.)

So in the game “The Haunted Staircase” Made by Ancientroboman whenever you see the monster your camera automatically goes to it until you move it.

All i’m wondering is how i would make this, I have no clue as to what he used.

Game: The Haunted Staircase - Roblox

5 Likes

I think Camera.CameraSubject is what you’re looking for.

Doesn’t camerasubject put the camera on the monster’s head?

I want the player to look at it.

No it won’t put the camera on the monster’s head. It will just revolve around it.


Are you trying to force the camera to look at it as if someone was shaking their head?

How do i explain this, I want the camera to stay on the player, But then the player looks at the monster

Maybe provide examples such as images, videos…?

At 1:50, It’s short but you can see his camera going to the monster before he pulls it away.

I think that’s some camera manipulation, don’t hesitate as there’s an article on it on the api reference!

1 Like

Alright, I’ll go check it out right now!

But what exactly is camera manipulation?

Scripting to move the camera around basically.

You could ask the creator themselves how they did this if they’re willing to share, seeing as they are a member of the forums and contribute every now and again. cc @Ancientroboman

1 Like

Oh, Thanks.
I should’ve asked him first.

Hello! What I did was :lerp() the CFrame of the camera to a new CFrame, which was just the current CFrame adjusted to look directly at a part.

If you don’t know what lerps are, I could go more in-depth with how I did what you see in the video.

Sorry about the late response, I don’t come on here all that frequently.

5 Likes

Isn’t lerp a type on tween, Or am i just talking dumb now, I haven’t really heared about lerp.

EDIT: Also when i try to adjust the camera’s CFrame to the part’s CFrame the camera doesn’t look at the part but at the sky, Because that’s where the part is facing.

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera

while true do
	wait(5)
	local Finish = workspace.ATM.Screen.CFrame
	local CameraMove = camera.CFrame:Lerp(Finish, 1)
end

I tried to make it but i guess i need to learn about lerp first.

I’m no expert but from my understanding, a lerp is like doing a single frame of a tween? Something like that. When you actually call the lerp, what you’ll want to do is:

local NewCFrame = CFrame.new(Camera.Position) * LookDirection
Camera.CFrame = Camera.CFrame:lerp(NewCFrame, 0.02)

You can always change the 0.02 to whatever you want, and you’ll need to define LookDirection as the direction you want the camera to face. You can do this with the following line:
local LookDirection = CFrame.new(Camera.CFrame.Position, ThingYouWantToLookAt.Position)

What we’re telling the script to do is take the camera, and do a single frame of a tween to a new cframe. The new cframe is the camera’s current position, but looking directly towards something, so it just rotates the camera a bit to face the part you want to look at.

2 Likes

Thanks a lot, I’ll try to learn more about lerp.
It seems helpfull maybe?

EDIT: WHAT SINCE WHEN IS CFrame.Position A THING

EDIT2: The camera only moves 3 pixels or so.

local Camera = workspace.CurrentCamera

local Goal = game.Workspace.ATM.Screen

while true do
wait(5)
local LookDirection = CFrame.new(Camera.CFrame.Position, Goal.Position)
local NewCFrame = CFrame.new(Camera.CFrame.Position) * LookDirection
Camera.CFrame = Camera.CFrame:lerp(NewCFrame, 0.02)
end

Yes, it will move your camera like 3 pixels or so. As I said, a lerp (kind of) does a single frame, or 0.05 seconds worth of a tween. As such, lerping once changes very little.

What I used in my game to continuously lerp over and over, was connect it to the RunService’s Stepped event. You can access this with the following lines:

local RS = game:GetService("RunService")
RS.Stepped:Connect(function()
     --lerp code
end)

RS.Stepped can only be called on a localscript, and it fires every single time the client gets a new frame. RunService also has some other useful events, like RenderStepped and Heartbeat, which fire before or after every time the server updates physics (kind of like a server-side frame).

To be more precise with how I used lerping, when something happens that I want to start pulling the player’s camera, I use this code:

local MyFunc = RS.Stepped:Connect(function()
     -- lerp code i mentioned earlier
end)

and then when I want to stop the lerping…

MyFunc:Disconnect()

This way, when I want to start pulling the user’s camera, it connects the Stepped event (which again, fires every frame the client has) and lerps in that function. Then, when I want to stop the camera-pulling, I disconnect the function.

1 Like

Runservice.Stepped, How often does it fire? Every frame maybe? I have no clue and would be glad if i knew so i could use it more often

Yes, every frame. It is better explained here (RunService | Documentation - Roblox Creator Hub) than I will be able to explain to you.

Oh thanks a lot, Also i’ve noticed my camera starts to like stutter? when i lock? It might be my code but i have no clue why, I can’t provide a GIF since gyazo isn’t working.

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local RS = game:GetService("RunService")

local Target
local Locked = false

local cameraOffset = Vector3.new(3, 3, 0)

local LockOn = RS.Stepped:Connect(function()
	if Locked == true then
		wait()
		local LookDirection = CFrame.new(Camera.CFrame.Position, Target.Position)
		local NewCFrame = CFrame.new(Camera.CFrame.Position) * LookDirection
		Camera.CFrame = Camera.CFrame:lerp(NewCFrame, 0.2)
	end
end)

Mouse.KeyDown:Connect(function(key)
	if key:byte() == 50 then
		if Locked == false then
			if Mouse.Target.Parent:FindFirstChild("HumanoidRootPart") then
				Target = Mouse.Target.Parent.HumanoidRootPart
				Locked = true
			end
								
		elseif Locked == true then
			Locked = false	
		end
	end
end)