Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

2 comments

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.

0

Whoops. z should be 20, not negative. Edited.

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

Here's the wiki article

1 comment

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....

This discussion is preserved as it was. New answers, comments, and votes are closed.