How can I make the camera move smoothly to the position of the script?
How do I make the camera move from where it stayed still to the new position?
and return to the position where the camera was before the new position?
whis is my code:
-- LOCALS --
-- Services
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local context = game:GetService("ContextActionService")
-- Player & Character
local plr = game:GetService("Players").LocalPlayer
-- Other
local Camera = game:GetService("Workspace").CurrentCamera
local mouse = plr:GetMouse()
local cameraDB = false
local zoomDB = false
-- Values
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(2,0,3.5)
-- CAMERA SETTING --
wait(0.01)
Camera.CameraType = Enum.CameraType.Scriptable
-- FUNCTIONS --
context:BindAction("CameraMovement", function(_,_,input)
xAngle = xAngle - input.Delta.x*0.4
yAngle = math.clamp(yAngle - input.Delta.y*0.4,-80,80)
end, false, Enum.UserInputType.MouseMovement)
rs.RenderStepped:Connect(function()
local c = plr.Character or plr.CharacterAdded:Wait()
local rootPart = c:FindFirstChild("HumanoidRootPart")
if c and rootPart then
local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
local cameraCFrame = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
local cameraFocus = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
end
end)```
As @AnotherScriptStudio mentioned, you can use TweenService another possibility is to use CFrame:lerp. You could use Vector3:lerp, but CFrame accounts for the rotation and the position of the camera, so usually it’s your best option. So something like this:
local targetCframe = CFrame.lookAt(Vector3.new(2,0,3.5), Vector3.new(0,0,0)) -- CFrame.lookAt(position, lookat position)
local step = 0
local rstep
rstep = rs.RenderStepped:Connect(function(t)
step += t
Camera.CFrame = Camera.CFrame:Lerp(targetCframe, step)
if step >= 1 then
rstep:Disconnect()
end
end)
I considered adding videos for that too but I decided not to because @skowdash mentioned they wanted to move the parts smoothly so I thought the transition options could be helpful to find the intended style that matches the game. In addition, I think that most people should try to learn tweening first because if you try to learn lerping first you could over-simplify a tween if you try to learn it later. Overall, both options should work.
Yes, I do agree that everyone should learn Tweening. However, as you said, it can be confusing to learn at first and also takes time to learn. Because Lerping is a simple concept to quickly learn, and the code shown in the original post displays an understanding of Iteration so I figured it’d be the easiest to implement in the pre-existing code.