Camera focus on a part?

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)

I have no clue what key:byte() does, also I am unfamiliar with Mouse functions as I use the UserInputService, but how I would go about setting this up is doing the following:

local LockOn = Instance.new("BoolValue")

Mouse.KeyDown:Connect(function(key)
   if WhateverYouWant == true then
      LockOn.Value = true
   else
      LockOn.Value = false
   end
end)

local MyFunc
LockOn.Changed:Connect(function()
   if LockOn.Value then
      MyFunc = RS.Stepped:Connect(function()
         --All of your lerping stuff
      end)
   else
      MyFunc:Disconnect()
   end
end)

Key:Byte() allows you to put a number which corresponds to a key, For instance 50 is CRTL.

Mouse is really bad though so im learning to use inputservice.

EDIT: Thanks, I’m gonna use this knowledge and stuff it inside my small brain to hopefully remember it for next time!

In order to do this same thing with UserInputService, you do this:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input, GHE)
--there is also an InputEnded if you want to detect when you stop pressing a key
--GHE stands for 'Game Handled Event', this will be true if you press keys during something like typing in chat, where you aren't necessarily doing something ingame

   if GHE then
      return
      --ignore if it's handled by the game
   end

   if Input.KeyCode == Enum.KeyCode.LeftControl then
      --do stuff
   end
end)

OO, it does seem way harder.

Any reasons as why it would be better then Mouse?

It’s not harder, just weird looking at first. I know enough to tell you that in general, UIS is better. I don’t know enough to really tell you why, sadly.

Oh alright, Have you perhaps got discord where you could go more into detail about userinput? Or do you not/do not want to give it, Since the responding time on here of me takes a while.

I honestly don’t really know anything about how different services work or compare to eachother, I just know how to use them to do stuff.

Oh alright then, Kinda sucks my camera is still stuttering, If you wanna know what i mean then join this game, Put your cursor over a NPC and press CTRL.

https://www.roblox.com/games/4138725330/IDK-A-NAME-BOIS-DEV

Could I see your current lerp script? I’ll put it in studio and mess around to see if I can change that twitching.