Perhaps add an offset that’ll keep the camera a fixed distance from the target and if it’s farther or closer than that target you can lerp the position back to a more desirable one?
This can become very complicated. For example, the distance away from head could be a clamped distance between 2 and 5 studs. Once the player is over 5 studs away, you would move the camera twords the player. If the player is less than two studs away, you move the camera away.
You also might have to account for objects too, so don’t forget that. Shoot rays from the players head to the camera, and if something is in the way, you need to adjust.
I created a script which works perfect except for the moving, I’m stuck on that part
local Player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
game:GetService("RunService").RenderStepped:Connect(function()
local DistanceFromHead = (workspace.Camera.CFrame.Position - Player.Character.Head.Position).magnitude
local Distance = CFrame.new(Player.Character.Head.Position) * CFrame.Angles(0, 1, 0) * CFrame.new(0,0,DistanceFromHead)
camera.CFrame = Distance
end)
local Player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Folder = game.Workspace.Folder
game:GetService("RunService").RenderStepped:Connect(function()
local DistanceFromHead = (workspace.Camera.CFrame.Position - Player.Character.Head.Position).magnitude
local Distance = CFrame.new(Player.Character.Head.Position) * CFrame.Angles(0, 1, 0) * CFrame.new(0,0,DistanceFromHead)
camera.CFrame = Distance
for i,v in pairs(Folder:GetChildren()) do
if camera:WorldToViewportPoint(v.Position) then
if (Player.Character.HumanoidRootPart.Position - v.Position).Magnitude <= 10 then
v.Parent = ReplicatedStorage.Folder
end
end
end
for i,v in pairs(ReplicatedStorage.Folder:GetChildren()) do
if (Player.Character.HumanoidRootPart.Position - v.Position).Magnitude > 10 then
v.Parent = Folder
end
end
end)
Thank you!, It might need some adjustments so I’ll get to work on that and tell you how it works out.
You are going to have to use lerp here. You have to face the player then move the camera to that distance away from the player.
-- RenderStepped
local TargetPosition = (Camera.CFrame.Position - Character.Head.Position).Unit * Distance + Player.Position-- the distance form player added to players position
Camera.CFrame = Camera.CFrame:Lerp(
CFrame.lookAt(
TargetPosition,
Character.Head.Position
),
DeltaTime * CameraLerpSpeed -- You can change this if you'd like
)
Distance is a number. It represents the magnitude of the direction vector. Since you already have a unit vector you can multiply it with a number to get a desired distance in studs the camera will attempt to lerp to.
In my case I put 10 for Distance and 5 for Camera Lerp Speed which is self explanatory.
Also I should have told you to put the Character.Head.Position instead of Player.Position
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local CameraLerpSpeed = 5
local Distance = 10
local Character = Player.Character
game:GetService("RunService").RenderStepped:Connect(function(DeltaTime)
local TargetPosition = (Camera.CFrame.Position - Character.Head.Position).Unit * Distance + Character.Head.Position
Camera.CFrame = Camera.CFrame:Lerp(
CFrame.lookAt(
TargetPosition,
Character.Head.Position
),
DeltaTime * CameraLerpSpeed
)
end)
It doesn’t dolly like your one. It just moves with breathing.
local Camera = workspace.CurrentCamera
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
local TargetPosition = (Camera.CFrame.Position - Character.Head.Position).Unit * 10 + Character.Head.Position-- the distance form player added to players position
Camera.CFrame = Camera.CFrame:Lerp(
CFrame.lookAt(
TargetPosition,
Character.Head.Position
),
deltaTime * 5 -- You can change this if you'd like
)
end)
Here is the full code.
Call it CameraScript and put it in StarterPlayerScript to get it to work.