Trying to wrap my head around math.rad. Radians?

So I’ve been playing around with this script that was previously posted in the Wiki:

local target = workspace.CameraTarget
local camera = workspace.CurrentCamera
camera.CameraType = Enum.CameraType.Scriptable
camera.CameraSubject = target
local angle = 0
 
while true do
	wait()
    camera.CoordinateFrame = CFrame.new(target.Position)  --Start at the position of the part
                           * CFrame.Angles(0, angle, 0) --Rotate by the angle
                           * CFrame.new(0, 0, 100)       --Move the camera backwards 5 units
	angle = angle + math.rad(.2)
end

It’s basically a camera manipulation script where it grabs the CurrentCamera and makes the camera spin around a target it’s focusing on.

A few seconds of this script running, it immediately stops at a certain angle. To my understanding, this may be due to a maximum number angles is only able to calculate, so I’m thinking that maybe when it reaches its 360 angle, it has to be reset to 0? I’m not entirely sure.

I was looking up the API source for math.rad, but I’m having a difficult time wrapping my head around it. What does radians exactly mean? How can I make the angle reset when it hits 360? For example

if angle == 360 then
	angle = 0
end

How would I translate this into radians? (Or any other way if possible.) Sorry if this question might seem basic.

3 Likes

This may help you understand:

image

  • Radians are another form of measuring angles, just like degrees are.
  • Degrees go up to 360 to be a full circle, while radians go up to 2pi (makes sense since a circumference is pi diameters, and radii are 1/2 of diameter).
  • You can convert degrees to radians using:
math.rad(degrees)

And similarly, for degrees:

math.deg(radians)
  • All CFrame operations, trigonometry functions, and anything else in Lua that ask for an angle are always in radians, so if you’re only comfortable with degrees, then you can convert using the function above.

So, for your if-statement you can do (remember, the variable angle is in radians):

if angle == math.rad(360) then
    angle = 0
end
11 Likes

Actually, this probably won’t work due to floating point errors. Adding a bunch of floating point values to each other might not end up being accurate, and so it’s unlikely angle will land exactly on math.rad(360), or 2π.

Use >=, or rather modulo:

angle = angle % math.rad(360)
3 Likes