-
What do you want to achieve?
Have to move the camera horizontally and vertically at the same time produce expected results (move both horizontally and vertically at regular speed). -
What is the issue?
The camera has an interesting result when doing so.
-
What solutions have you tried so far?
A lot of different methods for rotating the camera. Unfortunately, none have worked without flaws. This is the best I can get at the momment.
Here is all of the script code (although all of the actual camera work happens in the RenderStepped function)
local STORAGE = game:GetService("ReplicatedStorage")
local INPUT = game:GetService("UserInputService")
local SOUND = game:GetService("SoundService")
local RUN = game:GetService("RunService")
local ui = workspace:WaitForChild("screen"):WaitForChild("g")
local progress = 0
local movecamera = true
local lastrot = nil
local keyboardgaming = false
local onsides = {false,false,false,false}
local sides = {}
local function resetsides()
local function fromscale(X,Y)
return Vector2.new(math.floor(X * workspace.CurrentCamera.ViewportSize.X),math.floor(Y * (workspace.CurrentCamera.ViewportSize.Y + 36)))
end
sides[1] = {fromscale(1,0.1),Vector2.new(0,0)}
sides[2] = {fromscale(0.1,1),Vector2.new(0,0)}
sides[3] = {fromscale(1,0.1),fromscale(0,0.9)}
sides[4] = {fromscale(0.1,1),fromscale(0.9,0)}
end
resetsides()
workspace.CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(function()
resetsides()
end)
workspace.CurrentCamera:GetPropertyChangedSignal("CameraType"):Connect(function()
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
workspace.CurrentCamera.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0,math.rad(180),0)
end)
workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
workspace.CurrentCamera.CFrame = CFrame.new(0,0,0) * CFrame.Angles(0,math.rad(180),0)
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All,false)
game:GetService("RunService").RenderStepped:Connect(function()
game:GetService("Players"):ClearAllChildren()
end)
INPUT.InputBegan:Connect(function(key)
if movecamera then
if INPUT.KeyboardEnabled and keyboardgaming ~= nil then
if key.KeyCode == Enum.KeyCode.W then
keyboardgaming = true
onsides[1] = true
elseif key.KeyCode == Enum.KeyCode.A then
keyboardgaming = true
onsides[2] = true
elseif key.KeyCode == Enum.KeyCode.S then
keyboardgaming = true
onsides[3] = true
elseif key.KeyCode == Enum.KeyCode.D then
keyboardgaming = true
onsides[4] = true
end
end
if key.UserInputType == Enum.UserInputType.Gyro then
end
end
end)
INPUT.InputEnded:Connect(function(key)
if movecamera then
if key.KeyCode == Enum.KeyCode.W then
keyboardgaming = false
onsides[1] = false
elseif key.KeyCode == Enum.KeyCode.A then
keyboardgaming = false
onsides[2] = false
elseif key.KeyCode == Enum.KeyCode.S then
keyboardgaming = false
onsides[3] = false
elseif key.KeyCode == Enum.KeyCode.D then
keyboardgaming = false
onsides[4] = false
end
end
end)
INPUT.InputChanged:Connect(function(key)
if key.UserInputType == Enum.UserInputType.MouseMovement and keyboardgaming ~= true then
for x = 1,4 do
if key.Position.X > sides[x][2].X and key.Position.Y > (sides[x][2].Y - 36) and key.Position.X < sides[x][1].X + sides[x][2].X and key.Position.Y < (sides[x][1].Y + sides[x][2].Y - 36) then
keyboardgaming = nil
onsides[x] = true
else
onsides[x] = false
end
end
end
end)
if INPUT.GyroscopeEnabled then
INPUT.DeviceRotationChanged:Connect(function(rotation,rot)
if keyboardgaming ~= nil and keyboardgaming ~= true then
if not lastrot then
lastrot = rot
end
local delta = rot * lastrot:inverse()
local x,y,z = delta:toEulerAnglesXYZ()
delta = CFrame.Angles(x,-y,z)
workspace.CurrentCamera.CFrame *= delta
lastrot = rot
end
end)
end
RUN.RenderStepped:Connect(function(delta)
if movecamera then
local none = true
for x = 1,4 do
if onsides[x] then
none = false
local _,_,_,m00,m01,m02,_,_,m12,_,_,m22 = workspace.CurrentCamera.CFrame:GetComponents()
local angle = CFrame.fromOrientation(math.atan2(-m12, m22),math.asin(m02),math.atan2(-m01, m00))
if x % 2 == 0 then
workspace.CurrentCamera.CFrame *= CFrame.Angles(angle.X,math.rad(angle.Y + (x == 2 and 180 * delta or -180 * delta)),angle.Z)
else
workspace.CurrentCamera.CFrame *= CFrame.Angles(math.rad(angle.X + (x == 1 and 180 * delta or -180 * delta)),angle.Y,angle.Z)
end
end
end
if none and keyboardgaming == nil then
keyboardgaming = false
end
end
end)