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

Why is this not doing a smooth movement in the Y axis ?

Asked by 8 years ago

I've achieved making this go smoothly in the X axis, with no problems, but when you add the Y axis, it doesn't go as smooth as the X axis. Any help on making it look smoother like the X axis?

local cameraHeight = 12
local cameraZOffset = 20
local cameraXChase = 10
local cameraYChase = 20
local cameraSpeed = .25

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local function setupCamera()
    camera.CoordinateFrame = CFrame.new(Vector3.new(0,cameraHeight,cameraZOffset), Vector3.new(0,cameraHeight,0))
end
setupCamera()
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
    if player.Character and player.Character:FindFirstChild('Torso') then
        local playerX = player.Character.Torso.Position.X
        local playerY = player.Character.Torso.Position.Y
        local cameraX = camera.CoordinateFrame.p.X
        local cameraY = camera.CoordinateFrame.p.Y

        if cameraY - cameraYChase < playerY then
            camera.CoordinateFrame = camera.CoordinateFrame + Vector3.new(0, cameraSpeed, 0)
        end


        if cameraX - cameraXChase < playerX then
            camera.CoordinateFrame = camera.CoordinateFrame + Vector3.new(cameraSpeed, 0, 0)
        end
    end
end

RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)

2 answers

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
8 years ago

The camera jitters in the Y-axis whenever the player climbs a slope because the rate in which the Y changes is too slow (especially if the slope isn't steep). If the player jumps, you won't notice the jitter in the camera because the change in Y is big.

Instead of moving the camera at a fixed increment, try making it so that it goes part of the way (20%, for example) every frame.

I've also refactored and documented your script, which should take care of movement in all axes.

-- the offset of the camera in relation to the player. It is 10 studs to the left, 20 studs above, and 20 studs away from the player
local CamOffset = Vector3.new(10, 20, 20)
-- the direction in which the camera looks at. z = -1 means that it looks towards the player.
local CamLookDirection = Vector3.new(0, 0, -1)

local camera = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local RunService = game:GetService('RunService')

local function setupCamera()
    -- constructs the CFrame for the camera. You should probably check if HumanoidRootPart exists yet.
    camera.CoordinateFrame = CFrame.new(player.Character.HumanoidRootPart.Position + CamOffset, player.Character.HumanoidRootPart.Position + CamOffset + CamLookDirection)
end
player.CharacterAdded:connect(setupCamera)

local function onUpdate()
    if player.Character and player.Character:FindFirstChild('HumanoidRootPart') then
        -- lerps (linearly interpolates) the camera every frame. Each frame, it is 20% closer to its destination(0.2 == 20%).
        camera.CoordinateFrame = camera.CoordinateFrame:lerp( CFrame.new(player.Character.HumanoidRootPart.Position + CamOffset, player.Character.HumanoidRootPart.Position + CamOffset + CamLookDirection), 0.2 )
    end
end
RunService:BindToRenderStep('Camera', Enum.RenderPriority.Camera.Value, onUpdate)
0
The camera focuses above the player in the empty space where the character can't be seen, Even trying to modify the "local CamOffset = Vector3.new(10, 20, -20)" the Y axis to 5 it doesn't really focus on the character, it focuses on a way long upper space. iDarkGames 483 — 8y
0
Whoops. z should be 20, not negative. Edited. XAXA 1569 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Why not use interpolation? It's a method which allows smooth movement of the camera.

Here's the wiki article

0
As I see Interpolate works for defining a start Position and an end one, and I want the camera to follow the character's torso smoothly, it won't really fit well I suppose.... iDarkGames 483 — 8y

Answer this question