Hello, developers!
Before I start this topic, keep in mind that I am fairly new to CFrame and I don’t entirely understand how it works.
I have made this simple camera system:
local TargetCFrame = CFrame.new(Vector3.new(0,10,0))
local Camera = workspace.Camera
repeat Camera.CameraType = Enum.CameraType.Scriptable wait() until Camera.CameraType == Enum.CameraType.Scriptable
local UserInputService = game:GetService("UserInputService")
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Rotation = 0
local Directions = {
["W"] = CFrame.new(Vector3.new(0,0,-1));
["A"] = CFrame.new(Vector3.new(-1,0,0));
["S"] = CFrame.new(Vector3.new(0,0,1));
["D"] = CFrame.new(Vector3.new(1,0,0));
["Q"] = CFrame.new(Vector3.new(0,1,0));
["E"] = CFrame.new(Vector3.new(0,-1,0));
}
local CurrentKeys = {}
Mouse.KeyDown:Connect(function(KeyCode)
KeyCode = KeyCode:upper()
if not table.find(CurrentKeys, KeyCode) then
table.insert(CurrentKeys, KeyCode)
end
end)
Mouse.KeyUp:Connect(function(KeyCode)
KeyCode = KeyCode:upper()
if table.find(CurrentKeys, KeyCode) then
table.remove(CurrentKeys, table.find(CurrentKeys, KeyCode))
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
game:GetService("TweenService"):Create(Camera, TweenInfo.new(.5, Enum.EasingStyle.Quint), {CFrame = TargetCFrame* CFrame.Angles(0,math.rad(Rotation),0)}):Play()
for _, KeyCode in pairs(CurrentKeys) do
if Directions[KeyCode] then
TargetCFrame *= Directions[KeyCode]
end
end
local LeftX = Camera.ViewportSize.X/5
local RightX = (Camera.ViewportSize.X/5) * 4
local MouseX = Mouse.X
if LeftX > MouseX then
Rotation += 1
elseif MouseX > RightX then
Rotation -= 1
end
end)
However, when the script adds or subtracts CFrame depending on the keys being pressed, it is “global”… it’s moving along a global x/y/z axis. I assumed that multiplying one CFrame by another would create a local x/y/z axis.
Here’s a paint drawing to better express what I am trying to say here:
How can I adjust CFrame based on a current CFrame?