Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to detect how fast the camera is rotating??

Asked by 2 years ago
Edited 2 years ago

I will set a function which depends on how fast the camera is rotating each second. But, I've been stuck on this part.

Any help would be Highly Appreciated! Thanks in advance.

0
how fast it's moving or rotating? imKirda 4491 — 2y
0
Rotating Shounak123 461 — 2y

1 answer

Log in to vote
2
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago

Here is a code that prints camera rotation speed and movement speed, difference is that movement speed is not 0 when you are zooming in and out:

local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")

local currentCamera = Workspace.CurrentCamera

local previousCFrame = currentCamera.CFrame

RunService.RenderStepped:Connect(function()
  local cframe = currentCamera.CFrame

  local speed = (previousCFrame.Position - cframe.Position).Magnitude
  local _, rotation = (cframe * previousCFrame:Inverse()):ToAxisAngle()

  print(("Current movement speed of camera: %d"):format(speed))
  print(("Current rotation speed of camera: %d"):format(math.deg(rotation)))

  previousCFrame = cframe
end)

To get the movement speed you calculate distance between current camera position and camera position from previous frame, for angle I just stole the code here, since the result was in radians using math.deg I converted it to degrees, it's too hard of a math :o

0
Thank you so much!! Shounak123 461 — 2y
Ad

Answer this question