So I made a 2D camera script using a tween that puts the camera inside a part that is always 40 studas above the player and it’s focus point is another part that is 3 studs above the player,however it seems that the movement is really glitchy but I don’t know what makes it to be like that.
This is the whole script by the way:
local RS = game:GetService("RunService")
local ts = game:GetService("TweenService")
local Cam = game.Workspace.CurrentCamera
Cam.CameraType = Enum.CameraType.Scriptable
Cam:GetPropertyChangedSignal("CameraType"):Wait()
Cam.CameraType = Enum.CameraType.Scriptable
local lookAt = Instance.new("Part")
lookAt.Name = "lookAt"
lookAt.Parent = workspace
lookAt.Anchored = true
lookAt.Size = Vector3.new(0.1, 0.1, 0.1)
lookAt.CastShadow = false
lookAt.CanCollide = false
lookAt.Transparency = 1
local CamPart = Instance.new("Part")
CamPart.Name = "CamPart"
CamPart.Parent = workspace
CamPart.Anchored = true
CamPart.Size = Vector3.new(0.1, 0.1, 0.1)
CamPart.CastShadow = false
CamPart.CanCollide = false
CamPart.Transparency = 1
local Player = game:GetService("Players").LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
if not char then
return
end
local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
if not HumanoidRootPart then
return
end
RS:BindToRenderStep("Camera14", 201, function()
task.spawn(function()
while true do
local rootPos = HumanoidRootPart.Position
wait()
if true then
CamPart.Position = rootPos + Vector3.new(0, 40, 0)
wait()
lookAt.Position = rootPos + Vector3.new(0, 3, 0)
wait()
end
end
end)
-------------Making a Tween--------------------
while true do
wait()
local lookAtPosition = lookAt.Position
local CamPartPosition = CamPart.Position
local camgoal = {
CFrame = CFrame.new(CamPartPosition, lookAtPosition)
}
local info = TweenInfo.new(0, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local tween = ts:Create(Cam, info, camgoal)
tween:Play()
tween.Completed:Wait()
task.wait()
end
end)
if true then
print("Success")
end
You have a tween with duration 0 inside of a true loop which doesn’t make sense, you can just set the camera position. Also the amount of while true loops is confusing me, can’t you just put everything inside of 1 renderstepped?
In general I think you are way overthinking things, resulting in confusing and flawed code. For example you:
Create a part → Set the position of the part → Move the camera position to the part (3 steps)
when instead you could just move the camera position (1 step)
Also there is a lot of redundant and pointless code, for example:
You set the camera type of the camera to scriptable twice
If true statements which are completely pointless and can be removed ( The code inside of an if true statement will ALWAYS fire, so you can remove the if true logic and have the exact same result )
A statement which is under a while true do that will never fire
Multiple waits() that can be removed
Well,the reason why I prefered to do a tween instead of changing the camera position was beacause in my first 2D camera script attempt I did something similar to what you did (just changing the camera position) but I did’nt want to make the camera rotate and what I wanted to make is making the camera only move horizontally and vertically.
So I basically did’nt knew how to stop the camera from rotating.
Also changing the tween duration helped me with the buggy camera however the movement is still pretty glitchy as when I press a key it moves towards the other side for a few seconds or the character walks in circles.
The CFrame = CFrame.new(CamPartPosition, lookAtPosition) is the issue. Basically what it doing is rotating the camera to the lookAtPosition to look at the position, because you give it two Vector3 arguments and that will make the goal like CFrame = CFrame.lookAt(pos1, pos2)
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humRp = char:WaitForChild("HumanoidRootPart")
local Cam = workspace.CurrentCamera
Cam.CameraType = Enum.CameraType.Scriptable
local speed = 1 -- setting this value too high causes the lerp to jitter badly
local studOffset = 40 -- how many studs up u wanna go
local lastDelta = 0
game["Run Service"].RenderStepped:Connect(function(dt)
dt = (lastDelta + dt)/2 -- makes delta time smoooth
lastDelta = dt
Cam.CFrame = Cam.CFrame:Lerp( CFrame.new(humRp.Position.X,humRp.Position.Y + studOffset,humRp.Position.Z) * CFrame.Angles(math.rad(-90),0,0) , dt * speed)
end)
put this as a local script in starter player scripts
if u have questions ask me and if u dont want the stutter get rid of the lerp and set the cframe directly