How would I go about tweening the camera?

  1. What do you want to achieve? Basically I have 2 scripts that cahnges the camera of the player when they sit and I want to make the transition smoother by tweening it.

  2. What is the issue? I don’t understand how tweening works.

  3. What solutions have you tried so far? Researching and trying my best to understand tweening.

ServerScriptService (where the camera change event executes)

local Seat = game.Workspace.Chair.Seat
local RemoteEvent = game.ReplicatedStorage.RemoteEvents.CamSeat
local RemoteEvent2 = game.ReplicatedStorage.RemoteEvents.PlayerCam
local Players = game:GetService("Players")

local currentplayer = nil

local function onOccupantChanged()
	local humanoid = Seat.Occupant
	if humanoid then
		print("sit")
		local character = humanoid.Parent
		local player = Players:GetPlayerFromCharacter(character)
		if player then
			RemoteEvent:FireClient(player)
			currentplayer = player
			return
		end
	end
	if currentplayer then
		print("got up")
		local player = currentplayer
		RemoteEvent2:FireClient(player)
		currentplayer = nil
	end
end

Seat:GetPropertyChangedSignal("Occupant"):Connect(onOccupantChanged)

LocalScript (where the event’s code aka the camera change code is located)

local Seat = script.Parent

local RemoteEvent = game.ReplicatedStorage.RemoteEvents.CamSeat
local RemoteEvent2 = game.ReplicatedStorage.RemoteEvents.PlayerCam

local Camera = game.Workspace.CurrentCamera
local Camera1 = game.Workspace.CameraSeat

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

RemoteEvent.OnClientEvent:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = Camera1.CFrame
end)

RemoteEvent2.OnClientEvent:Connect(function()
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = char.Humanoid 
end)

Any help is very much appreciated and I am terribly sorry if tweening is easy to understand, I just started investing myself to coding. And if possible an explanation to the solution or an in depth article as the solution would mean a ton!

Disclaimer: I’d like to say that I’m not an expert. I work in this community as a scripter. I apologize in advance if I misrepresented any facts.

First thing first, you’ll need to understand what’s the word “Tweening” stands for.

Reference from Tweening to create animated action | Adobe

in Roblox. Tweening required 3 parameters, they are Object, Tween Info and Goal
Object is cleary clear mean what is it.
Tween Info Let me explain simply, so let’s think. You have a goal and you want to achieve within the time but there’s many many method or ways, that you can start from high,low or any position and still reached the same goal.

for Tween Info there’re 3 mainly properties that I used oftenly, are Time, Easing Style, Easing Direction.
From above I’d say that Easing Style is a way to achieve, Easing Direction is a starting point, and Time does the meaning by itself.

For more visualization:
I’ll go with a number that’s 0 and I want to reach to 10 in 10 seconds.
If you have some knowledges about Graphing like Linear, Logarithm, Expotential, Quad(Quadratic) and mores you’ll absolutely get this

continue. I’ll choose Linear Easing Style
and This is what it looks like in graphing
image
IMG Source: What is Linear Graph? Definition, Properties, Equation, Examples

And now for the example
`In the 0 second I’ll stay 0
In the 0.1 second I’ll go 0.1
In the 0.2 second I’ll go 0.2
In the 5 second I’ll go 5
and for The last Second I’ll reach the goal

Resources:

2 Likes

Thats an excellent introduction to tweening and far more in-depth than most responses on here.
You have the basics of what you need to ahieve in that you have the CFrames you want the camera to travel to as a destination, so now you just need to construct the tween:

local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.In, 0, false, 0) -- Values are Tween speed, style, direction, repeat count, reverse bool, delay)
local destCFrame = Camera1.CFrame  -- define the value you want to Tween
local tween = TweenService:Create(camera, tweenInfo, {CFrame = destCFrame }) -- create the Tween
tween:Play() -- play the tween

You can tween any numerical value, including Vector3, UDim2 and your CFrames. The above will accomplish your camera tween.

1 Like

Very informative! I’ll look into this more one I understand the basics. But for, now thank you so so much!

1 Like

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