I have been trying to script a custom camerascript for an RTS style camera for the last couple days. I use a part as the camera so I can easily manipulate properties like rotation using TweenService. By default the camera is rotated at a 45 degree angle, but I will add the option to use the Q and E buttons to rotate the camera around. Because of this rotation I need to move the camerapart relative to it’s lookvector instead of just moving it along the X or Z axis.
Here is my script so far:
(I don’t think the “zoom in and out” part is relevant to my problem, so you could skip that)
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local mouse = Player:GetMouse()
local scrollspeed = 1
local CameraPart = workspace.CameraPart
game.Workspace.Camera.FieldOfView = 50
-- place the camera high in the air, looking down at the ground
local startingPos = Vector3.new(0, 60, 50)
local downwardLookAngle = CFrame.Angles(-math.rad(40), 0, 0)
local offset = 10
local rotation = CFrame.Angles(0, math.rad(45), 0)
CameraPart.CFrame = CFrame.new(startingPos) * rotation
cam.CFrame = CameraPart.CFrame * CFrame.new(0, 0, offset)
-- create a function that moves the camera around
local moveValue = CFrame.new(0, 0, 0)
local speed = 0.5
local function onKeyPress(actionName, userInputState, inputObject)
if actionName == "moveCameraForward" then
if userInputState == Enum.UserInputState.Begin then
moveValue = CFrame.new(CameraPart.CFrame.LookVector * speed)
elseif userInputState == Enum.UserInputState.End then
moveValue = CFrame.new(0, 0, 0)
end
end
if actionName == "moveCameraBackward" then
if userInputState == Enum.UserInputState.Begin then
moveValue = CFrame.new(-CameraPart.CFrame.LookVector * speed)
elseif userInputState == Enum.UserInputState.End then
moveValue = CFrame.new(0, 0, 0)
end
end
if actionName == "moveCameraRight" then
if userInputState == Enum.UserInputState.Begin then
moveValue = CFrame.new(CameraPart.CFrame.RightVector * speed)
elseif userInputState == Enum.UserInputState.End then
moveValue = CFrame.new(0, 0, 0)
end
end
if actionName == "moveCameraLeft" then
if userInputState == Enum.UserInputState.Begin then
moveValue = CFrame.new(-CameraPart.CFrame.RightVector * speed)
elseif userInputState == Enum.UserInputState.End then
moveValue = CFrame.new(0, 0, 0)
end
end
end
-- Zoom in and zoom out
zoom = 8.5
UserInputService.InputChanged:Connect(function(input, GameProcessed)
if GameProcessed then return end -- NoOo ThAtS GuI !!!
if input.UserInputType == Enum.UserInputType.MouseWheel then
if input.Position.Z <= 0 then
for i = 1, 10 do
if zoom + 0.6 < 130 then
zoom = zoom + 0.6
wait()
else
break
end
end
else
for i = 1, 10 do
if zoom - 0.6 > -50 then
zoom = zoom - 0.6
wait()
else
break
end
end
end
end
if input.UserInputType == Enum.UserInputType then
if input.Position.Z <= 0 then
for i = 1, 10 do
if zoom + 0.6 < 130 then
zoom = zoom + 0.6
wait()
else
break
end
end
else
for i = 1, 10 do
if zoom - 0.6 > -50 then
zoom = zoom - 0.6
wait()
else
break
end
end
end
end
end)
game.ContextActionService:BindAction("moveCameraForward", onKeyPress, false, Enum.KeyCode.W)
game.ContextActionService:BindAction("moveCameraLeft", onKeyPress, false, Enum.KeyCode.A)
game.ContextActionService:BindAction("moveCameraBackward", onKeyPress, false, Enum.KeyCode.S)
game.ContextActionService:BindAction("moveCameraRight", onKeyPress, false, Enum.KeyCode.D)
-- Change camera
RunService.RenderStepped:Connect(function()
local c = CameraPart.CFrame
game.Workspace.CameraPart.CFrame = c * moveValue
cam.CFrame = CameraPart.CFrame * CFrame.new(0, zoom, 0) * downwardLookAngle * CFrame.new(0, 0, offset)
end)
Right now, when i press play the camera just moves along the Z or X axis, instead of taking the lookvector into account. Any help would be greatly aprecciated!