So this is my first time creating a custom camera script and I’m looking into it because I’ve been messing around with trying to create space-like physics for another game of mine. I was hoping to essentially allow the user to rotate fully around in any direction, without having a limit at the top and bottom, to sort of remove the player’s sense of up and down.
I managed to disable the default camera scripts and I got most of my custom camera working in a basic local script inside of StarterPlayerScripts, but whenever I look up or down past 81 degrees, it glitches out and doesn’t let me keep going.
Here’s what I mean: link
Here’s the part of the script that finds the normal to adjust the camera to:
-- Mouse Moved
uis.InputChanged:Connect(function(inputObj)
if inputObj.UserInputType == Enum.UserInputType.MouseMovement and rcDown then
-- Finding Deltas
local dx = inputObj.Delta.X
local dy = inputObj.Delta.Y
-- Creating Reference Part
local part = Instance.new("Part")
part.Transparency = 1
part.CanCollide = false
part.Anchored = true
part.CFrame = cam.CFrame
part.Orientation = Vector3.new(part.Orientation.X,part.Orientation.Y,0) + Vector3.new(-dy*0.5,-dx*0.5,0)
--[[if math.abs(part.Orientation.X) > 81 then
part.CFrame = cam.CFrame
part.Orientation = Vector3.new(part.Orientation.X,part.Orientation.Y,0) + Vector3.new(dy*0.5,dx*0.5,0)
end]] -- I used this in attempt to maybe flip the camera upside down once it reaches the glitching point, but all it managed to do was stop it from spazzing out as much.
camNormal = part.CFrame.LookVector
debris:AddItem(part,0)
end
end)
Here’s what it does when I add the part I commented out: link
I’m not sure if there’s a better way of finding the normal without creating a part and taking it’s LookVector, but either way, here’s the full script if needed:
-- Player Stuff
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local root = char:WaitForChild("HumanoidRootPart")
-- Character Added
player.CharacterAdded:Connect(function(Char)
-- Character Stuff
char = Char
root = char:WaitForChild("HumanoidRootPart")
end)
-- Mouse Stuff
local mouse = player:GetMouse()
-- Services
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local debris = game:GetService("Debris")
local ts = game:GetService("TweenService")
-- Camera Stuff
local cam = workspace.CurrentCamera
cam.CameraSubject = root
local zoom = 10
local maxZoom = 500
local minZoom = 1
local zInterval = 5
local camNormal = -cam.CameraSubject.CFrame.LookVector
-- Other
local rcDown = false
-- Right Click Down
mouse.Button2Down:Connect(function()
rcDown = true
end)
mouse.Button2Up:Connect(function()
rcDown = false
end)
-- Zooming
mouse.WheelForward:Connect(function()
if zoom - zInterval > minZoom then
zoom = zoom - zInterval
end
end)
mouse.WheelBackward:Connect(function()
if zoom + zInterval < maxZoom then
zoom = zoom + zInterval
end
end)
-- Updating Camera
local function tweenPos(cf,obj)
local info = TweenInfo.new(
0.05,
Enum.EasingStyle.Sine,
Enum.EasingDirection.InOut,
0,false,0
)
local properties = {
CFrame = cf
}
local tween = ts:Create(obj,info,properties)
tween:Play()
return tween
end
local oldT = nil
runService.Stepped:Connect(function()
if oldT and oldT.PlaybackState == Enum.PlaybackState.Playing then
oldT:Cancel()
end
oldT = tweenPos(CFrame.new(cam.CameraSubject.Position + camNormal*-zoom, root.Position),cam)
end)
-- Mouse Moved
uis.InputChanged:Connect(function(inputObj)
if inputObj.UserInputType == Enum.UserInputType.MouseMovement and rcDown then
-- Finding Deltas
local dx = inputObj.Delta.X
local dy = inputObj.Delta.Y
-- Creating Reference Part
local part = Instance.new("Part")
part.Transparency = 1
part.CanCollide = false
part.Anchored = true
part.CFrame = cam.CFrame
part.Orientation = Vector3.new(part.Orientation.X,part.Orientation.Y,0) + Vector3.new(-dy*0.5,-dx*0.5,0)
--[[if math.abs(part.Orientation.X) > 81 then
part.CFrame = cam.CFrame
part.Orientation = Vector3.new(part.Orientation.X,part.Orientation.Y,0) + Vector3.new(dy*0.5,dx*0.5,0)
end]] -- I used this in attempt to maybe flip the camera upside down once it reaches the glitching point, but all it managed to do was stop it from spazzing out as much.
camNormal = part.CFrame.LookVector
debris:AddItem(part,0)
end
end)
I looked around on some topics, and I found one topic from a while back that was talking about creating a custom camera like this, but I could never quite figure out what to do. One person there fully created such a camera and posted a roblox game file, but when I opened it up it was no longer functional.
I decided to look into the actual script for that camera and noticed they used various things like self and metatables that I’ve heard about, but I don’t really quite understand yet. I figured I’d just try doing it my own way with a simple local script, and here’s where I am now. Would it be more efficient to figure out this other person’s scripts and learn to recreate it in a way that works, or is there a relatively easy fix for what I have at the moment?