I don’t think you can do something like rad = math.rad where you can set a function as a variable. Also, when setting variables, if they go blue (they means that it is a keyword), then use a different name or the script may not work.
Instead of setting math.rad as a variable, just write it in below.
The reason this happens is because CFrame’s will rotate relative to the current rotation. This is what you want for the Y rotation, but not for the X rotation.
You’ll need to apply the X rotation before specifying any angle.
That’s not true. You can reference any function as a variable and call it as long as you have the right parameters. For a method like workspace:FindFirstChild(), you can do the following and it will still call correctly:
local findFirstChild = workspace.FindFirstChild
print(findFirstChild(workspace, “Baseplate”))
You can also overwrite blue colored variables without any issue, as so:
local p = print
local function print(...)
p(“print override:”,...)
end
print(“Hello World!”)
Hey, sorry. I actually realized I made a mistake. That code will rotate around 0, 0, 0 world space, so if you freecam outward, it won’t rotate around your current position.
Thank you very much. Is there some way that I’m able to clamp the Y axis, though?
I’ve thought of using math.clamp, but for some reason it never seems to work.
So I’m not really the best with math.clamp, and so some errors can happen.
I implemented the clamping feature, but now it does this. https://gyazo.com/f9a361eb25c6513ac09523b2e1d15c87
Is there something I’m doing wrong?
function Freecam()
Camera.CameraType = Enum.CameraType.Scriptable
local Offset = Camera.CFrame * CFrame.new(XDelta, 0, ZDelta)
local CurrentRot = Camera.CFrame - Camera.CFrame.Position
local ToClamp = Camera.CFrame:ToOrientation()
local ClampedY = clamp(ToClamp + YRot, 0, 180)
Camera.CFrame = CFrame.Angles(0, rad(XRot), 0) * CurrentRot * CFrame.fromOrientation(rad(ClampedY), 0, 0) + Offset.Position
end
Replace this line: ClampedX = math.clamp(ClampedX, -75, 75);
with ClampedX = math.clamp(ClampedX, rad(-75), rad(75));
I always forget to convert the degrees to radians when I clamp angles.