I was playing around with camera tweening and tried to tween the camera’s rotation.
The rotation did work, however during the tween, the camera doesn’t move with the player, it snaps back to the player after the tween is finished, but I would like to make it so that it moves with the player during the tween. How can I achieve this?
Relevant code:
local tweenService = game:GetService("TweenService")
local Camera = workspace.CurrentCamera
local tweenInfo1 = TweenInfo.new(
2,
Enum.EasingStyle.Back,
Enum.EasingDirection.Out
)
local uis = game:GetService("UserInputService")
local function tween()
local tween = tweenService:Create(Camera, tweenInfo1, {CFrame = Camera.CFrame * CFrame.Angles(0,0,math.rad(45))})
tween:Play()
end
uis.InputBegan:Connect(function(input, inchat)
if input.KeyCode == Enum.KeyCode.V then
tween()
end
end)
Tweening the CFrame’s rotation will still ask the tween to move it’s position. The camera doesnt stop moving with the player, it’s just that it’s also tweening the position. The position being when the tween started.
A CFrame includes both the position and orientation, and by tweening a CFrame, you are changing both the position and orientation, a fix for this should be by changing the property table (third parameter of :Create()) to something like:
Orientation doesn’t seem to work as it’s not a child of the camera nor the camera’s CFrame. Rotation is a child of the CFrame, however it brings the error “Unable to cast value to Object” whenever you try to change its value.
Well, my apologies, I didn’t realize that Camera doesn’t have an orientation property.
I didn’t find or could think of any way to solve your issue, and the only possible way I thought of to do something similar to what you want, is to change the CFrame’s rotation property through a loop, and of course, it won’t be as good as a tween since it won’t have an EasingStyle or an EasingDirection.
When interpolating using a loop, you can use the easing styles and easing directions of TweenService by using TweenService:GetValue().
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local tweenInfo1 = TweenInfo.new(
2,
Enum.EasingStyle.Back,
Enum.EasingDirection.Out
)
local currentTweenThread
local function tweenLoop(startRotation, endRotation, tweenInfo)
local tweenStartMoment = os.clock()
local normalizedTimeSpent = 0
while normalizedTimeSpent <= 1 do
local lerpAlpha = TweenService:GetValue(normalizedTimeSpent, tweenInfo.EasingStyle, tweenInfo.EasingDirection)
local interpolatedRotation = startRotation:Lerp(endRotation, lerpAlpha)
Camera.CFrame = interpolatedRotation + Camera.CFrame.Position
RunService.RenderStepped:Wait()
normalizedTimeSpent = (os.clock() - tweenStartMoment) / tweenInfo.Time
end
currentTweenThread = nil
end
local function tween()
if currentTweenThread ~= nil then
task.cancel(currentTweenThread)
end
currentTweenThread = task.spawn(tweenLoop, Camera.CFrame.Rotation, Camera.CFrame.Rotation * CFrame.Angles(0, 0, math.rad(45)), tweenInfo1)
end
UserInputService.InputBegan:Connect(function(input, inchat)
if input.KeyCode == Enum.KeyCode.V then
tween()
end
end)
Here’s an explanation by @Soliform about TweenService:GetValue().