Issue with tweening camera

(I’m really tired of this issue but it’s been annoying me for hours and I can’t find my logic error. Thought I’d post it here.)

Hello,

I’ve got a ziplining system that works moving the character, but always moves the camera to an odd location.

Video showing the issue:

Here’s my script:

local function processZipline(timeInSeconds:number, targetPos:Vector3, cancel: boolean?): nil --move the root part
	if cancel then
		tween:Cancel()
		return nil
	end
	
	local cam = workspace.CurrentCamera
	local camRotation = CFrame.fromEulerAnglesXYZ(cam.CFrame:ToEulerAnglesXYZ())
	local hrpRotation = rootPart.CFrame - rootPart.CFrame.Position
	
	local camOffset = cam.CFrame.Position - rootPart.CFrame.Position
	
	if db2 then return nil end
	
	targetPos += (rootPart.CFrame.LookVector * 2)
	
	--set position
	local targetCFrame = CFrame.new(targetPos) * hrpRotation
	local camTargetFrame = (targetCFrame + camOffset)
	
	--set up the tween
	local info = TweenInfo.new(timeInSeconds, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	
	--create rootPart tween
	tween = ts:Create(rootPart, info, {CFrame = targetCFrame})
	
	--create camera tween
	camTween = ts:Create(cam, info, {CFrame = (camTargetFrame + camOffset)})
	
	--stop current animations
	for _, anim in next, animator:GetPlayingAnimationTracks(), nil do
		anim:Stop(0)
	end
	
	--move rootPart to start offset
	rootPart.CFrame = CFrame.new(rootPart.CFrame.Position + (rootPart.CFrame.LookVector * 3)) * hrpRotation
	
	--play tween
	tween:Play()
	camTween:Play()
	track:Play()
	playing = true
	
	--wait until tween completes
	task.wait(info.Time)
	
	--stop animation
	track:Stop(0)
	
	--cleanup after tween
	if tween then
		tween:Destroy()
		camTween:Destroy()
	end
	
	playing = false
	db = false
	camTween = nil
	tween = nil
	
	db2 = true
	task.wait(1)
	db2 = nil
end

Any help is appreciated, this issue is really annoying me now. Also sorry for the lack of commenting, I normally comment more but I’m just trying to fix this before I fully comment it.

What’s the purpose of tweening the camera here?

More information like whether or not you use a custom camera system or you’re just looking for a specific effect could help.

Though my first theory could simply be the position you’re tweening to being other than what you expect or intend. Try printing out your final destination’s CFrame or creating a part at that location.
A few things like the camera’s offset being derived from the CFrame could be an issue with the rotation of the camera.

I put a part at the destination location and it’s always at an odd position. The purpose here is when I tween the HumanoidRootPart, the camera is always jittery so I decided to tween that as well, which was all working fine in first person because I was only tweening it to the same position. It’s only when I tried to account for the camera’s distance from the HumanoidRootPart that this issue started occuring.

I know it’s an issue with the target calculation somewhere, I just can’t figure out where. I’m using no custom camera system, just a tween.

Maybe a more simple fix would be to create transparent boxes just for the camera tween.
Tween between them two just as you have placed them including orientation.

That would make no difference, I would still need to calculate the target position and orientation which I am already doing. It would just add the extra hassle of making parts there, and as I previously said, I’m pretty sure the issue is somewhere in my calculations.

I may have read that wrong …

Using Completed
local TweenService = game:GetService("TweenService")
local TweenInfo = TweenInfo.new
local ts = TweenService

local function processZipline(timeInSeconds: number, targetPos: Vector3, cancel: boolean?): nil
	if cancel then
		if tween then tween:Cancel() end
		if camTween then camTween:Cancel() end
		return nil
	end
	
	local cam = workspace.CurrentCamera
	local camRotation = CFrame.fromEulerAnglesXYZ(cam.CFrame:ToEulerAnglesXYZ())
	local hrpRotation = rootPart.CFrame - rootPart.CFrame.Position
	local camOffset = cam.CFrame.Position - rootPart.CFrame.Position
	
	if db2 then return nil end
	
	targetPos += (rootPart.CFrame.LookVector * 2)
	
	local targetCFrame = CFrame.new(targetPos) * hrpRotation
	local camTargetFrame = (targetCFrame + camOffset)
	
	local info = TweenInfo(timeInSeconds, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	
	tween = ts:Create(rootPart, info, {CFrame = targetCFrame})
	camTween = ts:Create(cam, info, {CFrame = camTargetFrame})
	
	for _, anim in next, animator:GetPlayingAnimationTracks() do
		anim:Stop(0)
	end
	
	rootPart.CFrame = CFrame.new(rootPart.CFrame.Position + (rootPart.CFrame.LookVector * 3)) * hrpRotation
	
	tween:Play()
	camTween:Play()
	track:Play()
	playing = true
	
	camTween.Completed:Wait()
	tween.Completed:Wait()
	
	track:Stop(0)
	
	if tween then tween:Destroy() end
	if camTween then camTween:Destroy() end
	
	playing = false
	db = false
	camTween = nil
	tween = nil
	
	db2 = true
	task.wait(1)
	db2 = nil
end

This may help … gl

I managed to fix it myself, I just rewrote the whole thing making it easier to read and I got it working. Thank you both for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.