I am trying to script a rotating camera and so far, it does the job of rotating but it is too choppy. here is my script:
while wait(0.4) do
for i = 1, 360, 1 do
workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, i)
end
end
How do I make a uniformal rotation. Changing while wait(0.4) do
to some smaller value just causes the camera to rotate extremely fast, but not uniformal.
Try either tweening with TweenService
or connecting to some event such as RenderStepped
.
Tweening is fairly straight-forward, as it only needs setting the TweenInfo, and passing a table of properties and their desired values.
local tweenService = game:GetService("TweenService")
local camera = worksapce.CurrentCamera
local tweenInfo = TweenInfo.new(10, Enum.EasingSyle.Linear, Enum.EasingDirection.Out)
local rotating = true
while rotating do
local cameraTween = tweenService:Create(camera, tweenInfo, {
CFrame = camera.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, 360);
})
cameraTween:Play()
cameraTween.Completed:Wait()
end
Using RunService is more customizable, but requires keeping track of time. In the example below, we’re basically running the code inside the RenderStepped
connection every time right before the next frame is rendered. We can then see the time that has elapsed, and divide it by the total time to see what percentage we’re at (something between 0 and 1), and from there we can update the camera to that percentage of 360, which is a full rotation.
local runService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local rotating = true
local rotationTime = 5
local startTime = tick()
local connection = runService.RenderStepped:Connect(function()
if rotating then
local deltaTime = (tick() - startTime) % duration
camera.CFrame = camera.CFrame * CFrame.fromEulerAnglesXYZ(0, 0, (deltaTime / rotationTime) * 360)
end
end)
2 Likes
It outputs an error:
20:17:31.850 - Looped is not a valid member of Tween
20:17:31.851 - Stack Begin
20:17:31.851 - Script 'Players.bootsareme.PlayerScripts.CameraController', Line 19
20:17:31.851 - Stack End
I have edited the post; It should work now.