Player Camera that rotates to a specific part

I’m a beginner scripter, and I’m trying to make it so the player camera will rotate to a specific part smoothly. This is an example of what I’m trying to do, but I’m not sure where to start and what to do.

2 Likes

I will give you the code, and when you get better, you may understand it yourself.

local run = game:GetService'RunService'
local player = game.Players.LocalPlayer
local cam = workspace.Camera

local min = math.min
local pi = math.pi
local sin = math.sin
local lookingAt, heartBeat = nil, nil

local function lookAtPart(part, speed)
	lookingAt = part
	local currentPart = part

	local lookAtCFrame = CFrame.lookAt(cam.CFrame.p,part.Position)
	local t = 0

	cam.CameraType = Enum.CameraType.Scriptable
	repeat
		cam.CFrame = cam.CFrame:Lerp(lookAtCFrame,sin(t*pi/2))
		t += min(run.Heartbeat:Wait()*speed,1)
	until t >= 1 or lookingAt ~= currentPart
	if lookingAt == currentPart then
		cam.CameraType = Enum.CameraType.Fixed
	end
end

-- Example
lookAtPart(workspace.Part, 2) -- Looks at part at 2x speed

hopefully my code works

1 Like

I’m having trouble trying to make this script work. This script is a local script and I’m trying to make it so when you activate a ProximityPrompt then it will look at a part, this is what I have.


local run = game:GetService'RunService'
local player = game.Players.LocalPlayer
local cam = workspace.Camera

local min = math.min
local pi = math.pi
local sin = math.sin
local lookingAt, heartBeat = nil, nil

local function lookAtPart(part, speed)
	lookingAt = part
	local currentPart = part

	local lookAtCFrame = CFrame.lookAt(cam.CFrame.p,part.Position)
	local t = 0

	cam.CameraType = Enum.CameraType.Scriptable
	repeat
		cam.CFrame = cam.CFrame:Lerp(lookAtCFrame,sin(t*pi/2))
		t += min(run.Heartbeat:Wait()*speed,1)
	until t >= 1 or lookingAt ~= currentPart
	if lookingAt == currentPart then
		cam.CameraType = Enum.CameraType.Fixed
	end
end




ProximityPrompt.Triggered:Connect(function(player)
	
		lookAtPart(workspace.CameraLook, 1)
end)

I may be still doing something wrong, I’m not sure.

oh ok, i forgot to tell. Put the script i gave you in a local script, under StarterCharacterScripts. Then you need to work with RemoteEvents. Do you know RemoteEvents?

Shouldn’t it be

game:GetService("RunService") 

instead of

local run = game:GetService'RunService'

Both are valid. Parenthesis can be omitted in this case.

Not really, I am pretty new to RemoteEvents too

I figured it out, and it’s now working. Thank you!