Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I fix a laggy camera?

Asked by 9 years ago

So I made(am making) a game with a top down styled "scriptable" camera. You use WASD or arrows and it adds velocity in the corresponding direction. The camera is super laggy for some reason.

--Movement Script--

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local left = false
local right = false
local up = false
local down = false
local bVel = nil
local angle = game.Workspace.Angle
--[[       0.1 - 0.5 = Heavy Friction;     0.5 - 0.9 Low Friction]]
local friction = .9
ball = nil
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)




player.CharacterAdded:connect(function()
        wait()
        player.Character.Parent = game.Lighting
        for i,v in pairs(game.Workspace:GetChildren())do
                if v.Name == "Ball" then
                        if v:FindFirstChild('Owner') then
                                if v.Owner.Value == player.Name then
                                        ball = v
                                        bVel = ball.BodyVelocity
                                end
                        end
                end
        end




end)


mouse.KeyDown:connect(function(key)
        --if player.PlayerGui.Playing.Value == true and player.PlayerGui.inGame.Value ~= "" or player.PlayerGui.Playing.Value == false and player.PlayerGui.inGame.Value == "" then
        k = string.byte(key)
        if k == 97 or  k == 20 then
                left = true
        end    

        if k == 100 or k == 19 then
                right = true
        end    

        if k == 119 or k == 17 then
                up = true
        end

        if k == 115 or k == 18 then
                down = true
        end
        --else
        --      k = nil
        --end

end)


mouse.KeyUp:connect(function(key)
        --if player.PlayerGui.Playing.Value == true and player.PlayerGui.inGame.Value ~= "" or player.PlayerGui.Playing.Value == false and player.PlayerGui.inGame.Value == "" then
        k = string.byte(key)

        if k == 97 or k == 20 then
                left = false
        end

        if k == 100 or k == 19 then
                right = false
        end    

        if k == 119 or k == 17 then
                up = false
        end

        if k == 115 or k == 18 then
                down = false
        --end
        end
end)









function loop()
        if ball ~= nil then
        bVel.velocity = bVel.velocity * Vector3.new(friction,friction,friction)
        if left == true then
                        bVel.velocity = bVel.velocity - Vector3.new(2,0,0)
        end

        if right == true then
                        bVel.velocity = bVel.velocity + Vector3.new(2,0,0)
        end

        if up == true then
                        bVel.velocity = bVel.velocity - Vector3.new(0,0,2)
        end

        if down == true then
                        bVel.velocity = bVel.velocity + Vector3.new(0,0,2)
        end
        end
end

game:GetService("RunService").RenderStepped:connect(function()
        loop()
end)

--Camera Script--

local player = game.Players.LocalPlayer
local ball = nil
player.CharacterAdded:connect(function()
        wait()
        for i,v in pairs(game.Workspace:GetChildren())do
                if v.Name == "Ball" then
                        if v:FindFirstChild('Owner') then
                                if v.Owner.Value == player.Name then
                                        ball = v
                                end
                        end
                end
        end    
end)


function loop()
        if ball ~= nil then
                cam = game.Workspace.CurrentCamera
                cam.CameraSubject = ball
                cam.CameraType = "Scriptable"
                cam.CoordinateFrame = CFrame.new(ball.Position + Vector3.new(0,50,0)) * CFrame.Angles(-1.4,0,0)
        end
end

game:GetService("RunService").RenderStepped:connect(function()
        loop()
end)

Place

At first I thought it was the fact that I was using wait() instead of renderstepped, but that did nothing.

Thank you in advance :D

1 answer

Log in to vote
2
Answered by 9 years ago

Welp, no one answerd, but I figured it out, after some heavy digging. You have to use the interpolate method. It only works an scriptable cameras. What it does is tweens(smoothly transitions) from one spot to another.

Ad

Answer this question