Help with custom camera [SOLVED]

Hello! So basically I’m trying to make a very specific camera angle depending on where you are in relation to a part. I know that there probably is some kind of math that can get this done, but I personally have no clue. I want the player to always be looking at the part, but with an offset behind them. Here’s a picture showing what I’m talking about:


I already have an offset variable that sets the offset I want to the camera with a Vector3. Here’s the custom camera script (ismonster1 will equal true during this):

`task.wait()
local camera = workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Scriptable

local plr = game.Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()

local campart = game.Workspace.Camera
local maxTilt = 5
local ts = game:GetService("TweenService")
local angle = "front"

previousposition = campart.CFrame

local cando = true

local ismonster1 = false

local CameraShaker = require(game.StarterPlayer.StarterPlayerScripts.CameraShaker)

local anglevalues = {
	["right"] = Vector3.new(0,1.5,10),
	["left"] = Vector3.new(0,1.5,-10),
	["front"] = Vector3.new(-10,1.5,0),
	["top"] = Vector3.new(-2,15,0),
	["back"] = Vector3.new(10,1.5,0),
	["angledfront"] = Vector3.new(-10,7,0),
	["birds"] = Vector3.new(-2,25,0)
}
	
game:GetService("RunService").RenderStepped:Connect(function(deltatime)
	local offset = Vector3.new(0,0,0)
	local humroot = char:FindFirstChild("HumanoidRootPart")
	if cando == true then
		local asd = 0
		camera.FieldOfView = 72.5
		offset = (anglevalues[angle])
		local crouchscript = game.Players.LocalPlayer.Character:FindFirstChild("CrouchScript")
		if crouchscript ~= nil then
			if crouchscript.Crouch.Value == true then
				offset = offset - Vector3.new(0,2,0)
			end
		end
	end
	local previous100fpsvalues = {}
	
	local fps = 1 / game:GetService("RunService").RenderStepped:wait()
	
	table.insert(previous100fpsvalues,fps)
	
	if #previous100fpsvalues > 100 then
		table.remove(previous100fpsvalues,previous100fpsvalues[10])
	end
	
	local averagefps = 0
	
	for i,v in ipairs(previous100fpsvalues) do
		averagefps = averagefps + v
	end
	
	averagefps = averagefps / #previous100fpsvalues
	
	local valuee = 0.08 * (deltatime * (60 + averagefps))
	
	if cando == true then
		if humroot ~= nil then
			if ismonster1 == false then
				local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + offset), humroot.Position)})
				camera.FieldOfView = 70
				twen:Play()
			else
				local twen = ts:Create(campart, TweenInfo.new(valuee,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut), {CFrame = CFrame.new((humroot.Position + (offset + Vector3.new(0,1.5,0))), game.Workspace.CameraLookAt.Position)})
				camera.FieldOfView = 105
				twen:Play()
			end
		end --magnitude, roughness, fadeInTime, fadeOutTime, posInfluence, rotInfluence
	end
	--[[camera.CFrame = campart.CFrame * CFrame.Angles(
		math.rad((((mouse.Y - mouse.ViewSizeY / 1.5)/mouse.ViewSizeY)) * -maxTilt),
		math.rad((((mouse.X - mouse.ViewSizeX / 1.5)/mouse.ViewSizeX)) * -maxTilt),
		0
	)]]
end)`

If you need anymore information, reply below :slight_smile: (this is a repost)

1 Like

Have you tried using cframe.lookat?

You could try a combination of Camera.CameraSubject, Humanoid.CameraOffset and Enum.CameraType.Scriptable.

(if you already have these sorry, i couldnt find them in the script)

You can use CFrame.lookAt like @cool_legend999 said and to offset the camera behind the player, you would use the LookVector of the CFrame you get with lookAt. If you want camera to follow a bit slower you could use Lerp instead of tweening:

local speed = 0.08 --this is the speed at which camera follows the part
local offset = 10 --offset from the player
local yOffset = Vector3.new(0,1.5,0) --this is so the camera is a little higher up

camera.CameraType = Enum.CameraType.Scriptable

run.RenderStepped:Connect(function(dt)
	local newCF = CFrame.lookAt(player.Character.HumanoidRootPart.Position + yOffset, part.Position)
	camera.CFrame = camera.CFrame:Lerp(newCF - newCF.LookVector * offset, speed)
end)

Basically you would first need to get a CFrame to look at the target from the players position, then add a CFrame to it with your offset.

Something like so:

camera.CFrame = CFrame.new(player.Position,lookat.Position)*CFrame.new(0,0,5)

Where camera is obviously your camera, player is the player’s head or any part in general and lookat is your target part.

Here is the result:

1 Like

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