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

Is there a way to have a smoother camera scroller?

Asked by 5 years ago
Edited 5 years ago

In my game, there's a mechanic where you can edit a wall and put stuff on it.

I haven't done any progress on the editing but I did with my camera scrolling.

So, my goal is to make it so the camera goes into a side-scroller state.

Temporarily, I made a part which lets me go into this state.

This is the script for the part which puts the player's camera to the CFrame of the part.

local cam = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local rp = game:GetService("ReplicatedStorage")

local debounce = false

workspace.Interactables.CameraPart.Touched:Connect(function(hit)
    if hit.Parent == game:GetService("Players").LocalPlayer.Character and not debounce then
        cam.CameraType = Enum.CameraType.Scriptable
        cam.CFrame = workspace.CameraStart.CFrame
        for _, Button in pairs(rp.GuiButtons:GetChildren()) do
            if Button.Name == "EditorButton" then
                Button:Clone().Parent = player.PlayerGui
            end
        end
    end
end)

I didn't know a method on how to say if the player has the button to go back so I used debounce, there's probably a better way.

Now, in this script, I made two versions of the camera script. But, both of them doesn't meet my expectations of a side scroller

Here's the script:

-- Services
local uis = game:GetService("UserInputService")

-- Variables
local cam = workspace.CurrentCamera
local startingPosition = workspace.CameraStart
local wall = workspace.EditingWall

--[[ Correct Way, But not smooth
uis.InputBegan:Connect(function(input,gameprocessed)
    if input.KeyCode == Enum.KeyCode.A then
        cam.CFrame = cam.CFrame * CFrame.new(-1,0,0)
    elseif input.KeyCode == Enum.KeyCode.D then
        cam.CFrame = cam.CFrame * CFrame.new(1,0,0)
    elseif input.KeyCode == Enum.KeyCode.S then
        cam.CFrame = cam.CFrame * CFrame.new(0,-1,0)
    elseif input.KeyCode == Enum.KeyCode.W then
        cam.CFrame = cam.CFrame * CFrame.new(0,1,0)
    end
end)
]]--

--[[ Not the solution I was looking for
uis.InputBegan:Connect(function(input,gameprocessed)
    if input.KeyCode == Enum.KeyCode.A then
        cam:Interpolate(cam.CFrame*CFrame.new(-1,0,0), wall.CFrame, 1)
    elseif input.KeyCode == Enum.KeyCode.D then
        cam:Interpolate(cam.CFrame*CFrame.new(1,0,0), wall.CFrame, 1)
    elseif input.KeyCode == Enum.KeyCode.S then
        cam:Interpolate(cam.CFrame*CFrame.new(0,-1,0), wall.CFrame, 1)
    elseif input.KeyCode == Enum.KeyCode.W then
        cam:Interpolate(cam.CFrame*CFrame.new(0,1,0), wall.CFrame, 1)
    end
end)
]]--

Now, I have a problem where if I went back to the player, the script above still activates which I don't want to happen.

This is the script for the go back to player script

local player = game.Players.LocalPlayer
local char = player.Character or player.CharactedAdded:Wait()
local cam = workspace.CurrentCamera

script.Parent.MouseButton1Down:Connect(function()
    cam.CameraType = "Scriptable"
    cam.CameraSubject = char:WaitForChild("Humanoid")
    cam.CameraType = "Custom"
    script.Parent.Parent.Parent:Destroy()
end)

Edit: Tried using lerp on the camera script.

uis.InputBegan:Connect(function(input,gameprocessed)
    if input.KeyCode == Enum.KeyCode.A then
        for i=0,1,0.1 do
            cam.CFrame:lerp(cam.CFrame*CFrame.new(-1,0,0), i)
        end 
    elseif input.KeyCode == Enum.KeyCode.D then
        for i=0,1,0.1 do
            cam.CFrame:lerp(cam.CFrame*CFrame.new(1,0,0), i)
        end 
    elseif input.KeyCode == Enum.KeyCode.S then
        for i=0,1,0.1 do
            cam.CFrame:lerp(cam.CFrame*CFrame.new(0,1,0), i)
        end 
    elseif input.KeyCode == Enum.KeyCode.W then
        for i=0,1,0.1 do
            cam.CFrame:lerp(cam.CFrame*CFrame.new(0,1,0), i)
        end 
    end
end)
-- PS. It doesn't work. >_<

I'm probably wrong with implementing lerp. I don't know.

Thanks for the help in advance!!! :33

0
make more cframes. that might help imaginevreything -50 — 5y
0
that is so vague. Troyonix 5 — 5y

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago

There is an event from RunService called RenderStepped that will fire every "frame" when used in a LocalScript. You are able to use this to move the camera smoothly whilst the keys are being held. RenderStepped also gives an argument that is the amount of time elapsed since the last RenderStep, so you can multiply that by your movement to make sure they camera maintains the same speed during lag spikes:

local CamSpeed = 5

local YSpeed = 0
local XSpeed = 0

game:GetService("RunService").RenderStepped:Connect(function(Step)
    Cam.CFrame = Cam.CFrame + Vector3.new(XSpeed,YSpeed,0)*CamSpeed*Step
end)

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(Key,IsTyping)
    if not IsTyping then
        if Key.KeyCode == Enum.KeyCode.A then
            XSpeed = -1
        elseif Key.KeyCode == Enum.KeyCode.D then
            XSpeed = 1
        elseif Key.KeyCode == Enum.KeyCode.W then
            YSpeed = 1
        elseif Key.KeyCode == Enum.KeyCode.S then
            YSpeed = -1
        end
    end
end)
UIS.InputEnded:Connect(function(Key,IsTyping)
    if not IsTyping then
        if Key.KeyCode == Enum.KeyCode.A or Key.KeyCode == Enum.KeyCode.D then
            XSpeed = 0
        elseif Key.KeyCode == Enum.KeyCode.W or Key.KeyCode == Enum.KeyCode.S  then
            YSpeed = 0
        end
    end
end)

Keep in mind you have to set the Cam variable to the camera and make the CameraType Custom. If the movement doesn't start and stop exactly when you want it, you can mess around with the UIS stuff.

0
I didn't know that you had to use RunService. I haven't used that in any of my scripts. Troyonix 5 — 5y
Ad

Answer this question