I want to making so when a rig with “Enemy” tag getting in a certain range
the camera will pan to focus the middle of the distance from that Rig to the player so both will be inside the view of the camera to prevent an enemy getting a cheap shot on the player off camera
normal view:
(The closet) Enemy rig spotted:
my current camera code:
-- Services
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Variables
local Player = PlayerService.LocalPlayer
local CurrentCamera = workspace.CurrentCamera
local Angle = -50
-- Ignored prop part
local CS = game:GetService("CollectionService")
local Tag = CS:GetTagged("Prop")
-- Cause Camera Lag
local lagFactor = 0.04 -- adjust lag (1=No Lag, 0 = Broken, 0.01 = Lot of lag)
local lastPosition = Vector3.new()
local lastRotation = CFrame.new()
CurrentCamera.CameraType = Enum.CameraType.Scriptable
RunService.RenderStepped:Connect(function(deltaTime)
if Player.Character then
local humRootPart = Player.Character:FindFirstChild("HumanoidRootPart")
if humRootPart then
-- lock the camera and make it follow player
local targetPosition = humRootPart.Position + Vector3.new(0, 20, 15)
local targetRotation = CFrame.Angles(math.rad(Angle), 0, 0)
-- apply cam lag
lastPosition = lastPosition:Lerp(targetPosition, lagFactor)
lastRotation = lastRotation:Lerp(targetRotation, lagFactor)
CurrentCamera.CFrame = CFrame.new(lastPosition) * lastRotation
-- cam collision to prevent the cam from clipping through the wall
local cameraRay = Ray.new(Player.Character.Torso.Position, CurrentCamera.CFrame.Position - Player.Character.Torso.Position)
local Ignore = {Player.Character}
-- check if part has the tag "prop" if yes, don't collide the cam with the part
for i, Object in pairs(Tag) do
if Object:IsA("Part") then
table.insert(Ignore, Object)
end
end
local HitPart, HitPosition = game.Workspace:FindPartOnRayWithIgnoreList(cameraRay, Ignore)
CurrentCamera.CFrame = (CurrentCamera.CFrame - (CurrentCamera.CFrame.Position - HitPosition)) + (Player.Character.Torso.Position - CurrentCamera.CFrame.Position).Unit
end
end
end)

